Search code examples
apache.htaccess

htaccess if statement


I have the following line in my .htaccess file to select which version of PHP to use:

AddType x-httpd-php53 .php

This works great in the live environment but doesn't apply to the test environment and breaks the site.

Is there a way I can put it in an if statement or something by IP of the server or URL of website or something so that it only comes into effect in the live environment?


Solution

  • With Apache 2.4, it is easy with <If>/<Else> directives (on %{HTTP_HOST}?).

    <If "%{HTTP_HOST} == 'foo'">
        # configuration for foo
    </If>
    <Else>
        # default configuration
    </Else>
    

    For Apache 2.2 and earlier, I would add a parameter to the startup command line of Apache (-D option) in one of the two environments then test if it is present or not via <IfDefine>.

    To do this on Windows, with Apache started as a service, modify key registry HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\Apache2.<VERSION>\ImagePath
    by appending -DFOO. Then, you can write:

    <IfDefine FOO>
        # configuration for foo
    </IfDefine>
    <IfDefine !FOO>
        # default configuration
    </IfDefine>