php.htaccessmod-php

Detect support for php_value / php_flag in .htaccess to suppress errors - PHP CGI mode - mod_php


I use php_value and php_flag rules in .htaccess such as:

php_value upload_max_filesize 100M

But this causes an error when the server is running in CGI mode instead of Apache mode, and instead I have to use php.ini rules such as:

upload_max_filesize = 100M

My code libraries needs to work for either mode, without manual changes for each server.

It took me a while to find a solution to allow both methods to be in place, and as it was hard to find I thought I'd post my solution here.


Solution

  • You can detect mod_php in .htaccess and ensure that these directives are only attempted when supported. In .htaccess:

    <IfModule mod_php5.c>
       php_value upload_max_filesize 100M
       php_flag ...
    </IfModule>
    

    Note: Depending on your setup you might need to change mod_php5.c to mod_php4.c or mod_php.c.

    In addition, you can manually stop the redundant php.ini file from being publicly accessible when in php mode using the following in .htaccess:

    <Files ~ "\.ini">
       Order allow,deny
       Deny from all
    </Files>
    

    Hope this saves you some time!