Set PHP configuration options with an Apache .htaccess file

PHP has a large number of configuration option which can be set in the php.ini file, Apache <virtualhost> blocks, .htaccess files and ini_set(). This post looks at how to set PHP configuration options with Apache’s .htaccess files.

Note that if an option can be set in an .htaccess file it can usually (but not always) be set using the ini_set() function. I’ll cover that function in a later post. The complete list of PHP configuration options can be found in the PHP manual.

Note also that only options labelled PHP_INI_PERDIR or PHP_INI_ALL from that list can be set in .htaccess file.

If for example you wanted to change the setting for auto_prepend_file, which makes a file be included before the PHP script, you would add this to your .htaccess file, where “prepend.inc.php” is the file to be prepended to your script:

php_value auto_prepend_file prepend.inc.php

To change the maximum file size that can be uploaded set the upload_max_filesize value. It defaults to 2M but you can change it to e.g. 5M like so:

php_value upload_max_filesize 5M

Note that although both of the above options can be set using ini_set() they will have no effect because the actions of these options occur before your script is parsed. Putting them in an .htaccess files means they will work.

There are a large number of PHP options that can be configured in an .htaccess file. Read the List of php.ini directives and Description of core php.ini directives PHP manual pages for more details about each option.

I’ve written a follow up post which looks at setting PHP_INI_SYSTEM options in an Apache virtualhost settings. Read more here: Set PHP configuration options in an Apache virtualhost

Add Comment