Apr
2013
Using SetEnv and SetEnvIf together in Apache
A co-worker had a problem with using SetEnv
and SetEnvIf
in his Apache configuration, where the SetEnvIf
didn’t seem to be working. What he was running into was the fact that those two commands are implemented in separate Apache modules which happen to run in different request phases.
In general the code was something like:
SetEnv WHICHENV prod SetEnvIf Host eval WHICHENV=eval SetEnvIf Host dev WHICHENV=dev
so the application can tell whether it’s on the development, evaluation, or production server. That’s how you would write the code as a program, but this example will end up always having the WHICHENV
variable set to prod
.
If your Apache has the server-info
handler enabled (usually mapped to /server-info
) you can use that to see that the SetEnv
is implemented in mod_env, which runs in the Fixups phase, while SetEnvIf is implemented in mod_setenvif, which runs in the Post-Read Request and Header Parse phases, which means the SetEnvIf
commands are run before the SetEnv
command.
The easiest workaround is to change the SetEnv
to SetEnvIf
so the lines get executed in order:
SetEnvIf Host ".*" WHICHENV=prod SetEnvIf Host eval WHICHENV=eval SetEnvIf Host dev WHICHENV=dev
AR
July 12, 2015 at 9:42 pm (9 years ago)Thanks for posting this. I ran into this exact problem today and was pulling my hair out until I found your post. I may have never figured it out without this!
Dimitar Ivanov
August 2, 2016 at 12:15 pm (8 years ago)Frank, thank you so much for this post. I was trying to set the HTTPS variable using the SetEnv and SetEnvIfNoCase with no luck until I found your post.
lkraav
November 26, 2016 at 4:09 pm (7 years ago)Here’s another one who found this useful.