Set PHP configuration options with an Apache .htaccess file
Posted April 16th, 2009 in Apache and PHP (Updated May 28th, 2009)
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
Related posts:
- Get Apache to parse .html files as PHP (Sunday, January 10th 2010)
- Log PHP errors with log_errors and error_log (Thursday, May 14th 2009)
- Automatically append or prepend files in a PHP script (Monday, May 11th 2009)
- Apache does not log HTTP status set with PHP header when using mod_rewrite in .htaccess files (Sunday, April 5th 2009)
- Get a list of all available classes with PHP (Thursday, July 10th 2008)
- PHP is not showing any error messages (Tuesday, June 10th 2008)

Comments
blog comments powered by Disqus