Using apache's IfDefine for dev and production environments

by jenny on 10 March 2012 - 11:25pm in

If you need different .htaccess directives depending on your development environment, you can do it like so:

<IfDefine dev>
# do something for development environment
</IfDefine>

<IfDefine !dev>
# do something for everywhere but the development environment
</IfDefine>

The trick is setting the variables. Contrary to common sense, using SetEnvIf does NOT work:

SetEnvIf Host ^dev.* dev

Instead, you have to pass the flag to apache on startup. In Ubuntu, add the following to the end of /etc/apache2/envvars:

export APACHE_ARGUMENTS='-D dev'

Now restart your Apache server like so (sudo apache2ctl restart didn’t work for me):

sudo apache2ctl stop
sudo apache2ctl start

envars file name

Very useful thank you very much

But I just noticed when using your instructions that the filename should be envvars not envars (two 'v's). I'm not sure if this has changed with different versions but it is true on Apache 2.4.6

Yep!

Yep, that was a typo -- fixed, thanks.