Gentoo Wiki




in smaller environments where there are only a few developers and a single product there is seldom a need for different values of a php.ini setting. however, depending on how esoteric some of the developers in your group are, or how many projects you have going simultaneously, or how many devs you have working on the same box or you have production and development running on the same box or whatever; you may at some point find a need to have different values for certain php.ini settings for different environments hosted on a single server. the good news is, if youre running apache, php has integrated support for this need.
.htaccess overrides are especially attractive in shared hosting environments where there is a high likelihood different clients will want different settings for php.ini directives.


Apache Configuration

to get started you need to enable .htaccess overrides in apache. there are many subtle caveats to setting up .htaccess usage. primarily these revolve around apache being configured to search recursively through directories above and below a particular directory where you intend to have a .htaccess file. searching for .htaccess files actually occurs on every request, unlike httpd.conf, which is loaded once when the server is started. thus, a high overhead can be incurred if apache is configured to search for .htaccess files recursively. the reader is left to investigating a proper (or suitable) .htaccess configuration, but here a sample is supplied. at a minimum you need to have AllowOverride set to Options or All per the php manual

    <Directory /home/phpExpert/working/www>
        Options -Indexes FollowSymLinks
        AllowOverride Options
        Order allow,deny
        Allow from all

override availability

not all php.ini settings can be configured in .htaccess. there is a hierarchy that dictates where values can be set and where they cannot. here is the chart, straight from the php manual

Constant Value Meaning
PHP_INI_USER 1 Entry can be set in user scripts or in Windows registry
PHP_INI_PERDIR 2 Entry can be set in php.ini, .htaccess or httpd.conf
PHP_INI_SYSTEM 4 Entry can be set in php.ini or httpd.conf
PHP_INI_ALL 7 Entry can be set anywhere

you can reference these in the php.ini directive listing, directly above the chart ive just shown, here.


finally, we can take a look at the syntax of a php.ini override setting in a .htaccess file. the syntax is quite simple, i found it listed in an article on the web.
the format is

php_value PHP-variable value

here are some examples

php_flag log_errors Off
php_value auto_prepend_file "framework/autoload.php"

notice, there are 2 different 'directive types' (for lack of a proper term) in this example. php_flag specifies boolean php.ini directives; php_value specifies non-boolean php.ini directives


if you use php_flag to specify a directive that is non-boolean or vice-versa, you are likely to encounter strange errors, such as the following:

Warning: Unknown: failed to open stream: No such file or directory in Unknown on line 0

Fatal error: Unknown: Failed opening required '0' (include_path='.:/mxn/php:/usr/share/php5:/usr/share/php') in Unknown on line 0


a distributed configuration is a powerful mechanism to enable support for a wide range of varying users on a single system. apache implemented it and php is leveraging it. even if you dont run a shared hosting environment at an isp, you still might find overriding php.ini values useful to create multiple environments to allow you to experiment with things like php debuggers, opcode cachers, autolaoding and more without impacting your stable branch.

Retrieved from ""

Last modified: Fri, 05 Sep 2008 10:37:00 +0000 Hits: 5,074