Apache – How to Have A Conditional .htaccess Block

.htaccessapacheapache2

This is an Apache question you've probably come across before. I want to have one source package that I can deploy to my workstation, my staging server, and my production server, but for it to load different .htaccess settings based on what the URL was.

Note that I was using a kludge with an IfModule call, but that won't work with our new production server because it shares all the same modules as my staging server.

Note I need to bundle SetEnv with these rewrites. Currently if you use RewriteCond, it only ties to the following RewriteRule, but not the SetEnv underneath.

Best Solution

Instead of using SetEnv, use the environment variable setting capabilities of RewriteRule itself:

RewriteCond %{HTTP_HOST} =foo.com
RewriteRule ^ - [E=VARNAME:foo]

RewriteCond %{HTTP_HOST} =bar.com
RewriteRule ^ - [E=VARNAME:bar]

Although I prefer doing this sort of thing by passing flags to the httpd process at startup and looking for them using IfDefine blocks.

<IfDefine FOO>
SetEnv VARNAME foo
</IfDefine>

<IfDefine BAR>
SetEnv VARNAME bar
</IfDefine>
Related Question