I'm trying to include a config file inside an Apache 2.4 <VirtualHost>
based on the presence of an environment variable.
Inside the VirtualHost
declaration, I set the VIEWMODE
environment variable as such:
Define virtualhost_config "${virtualhost_path}/conf/virtualhost.conf"
<VirtualHost *:80>
SetEnv VIEWMODE demo
Include "${virtualhost_config}"
</VirtualHost>
Inside the included config file, I now have this conditional inside the <Directory>
block:
<If "env('VIEWMODE') == 'demo'">
RewriteRule (.*) http://www.apple.com/ [L,R=302]
</If>
However, I can't seem to get this to work. The conditional RewriteRule
is ignored.
What am I missing?
I solved this by setting an Apache variable, which then fed both the SetEnv
directive as well as the <If>
block. Unfortunately, the <If>
block itself seemed to cause issues with the processing order of directives inside it (e.g. ServerAlias not allowed here
), but <IfDefine>
did not have this problem (using <IfDefine>
only worked for me because VIEWMODE
was binary). The final solution looked something like this:
Define environment production
Define viewmode demo
<VirtualHost *:80>
SetEnv ENVIRONMENT ${environment}
<IfDefine viewmode>
SetEnv VIEWMODE ${viewmode}
Include "${virtualhost_path}/conf/demo-configuration.conf"
</IfDefine>
</VirtualHost>
UnDefine environment
UnDefine viewmode
An important caveat is that Apache variables are global, so if the same variables might be used in subsequent Virtual Hosts, make sure to UnDefine
them at the end of each config.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With