Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I've got a RewriteCond in a .htaccess similar to:

RewriteCond "%{ENV:FOO}" =bar

But the ${ENV:FOO} is being expanded to the value of FOO, it's always blank.

For example, if I SetEnv FOO "bar" just before the RewriteCond, then crank up the RewriteLogLevel, I get this in my rewrite log:

RewriteCond: input='' pattern='=bar' => not-matched

What am I doing wrong here? How can I make environment variables expand inside of RewriteConds?

Also, if I add a silly RewriteRule like RewriteRule .* %{ENV:FOO}, I can see in the rewrite log that the variable is being correctly expanded there.

My Apache version is 2.2.17.

Edit: I've found another reference to this problem. It seems like a “solution” is to use SetEnvIf instead of SetEnv… =\

Additionally, it doesn't seem like the location of the SetEnv has any effect. I've put it both in httpd.conf and in the .htaccess, and neither work.

share|improve this question
up vote 3 down vote accepted

Ah, here we go. From the SetEnv documentation (under "caveats"):

The SetEnv directive runs late during request processing meaning that directives such as SetEnvIf and RewriteCond will not see the variables set with it.

So, for now, I guess I'm using:

SetEnvIf _ .* FOO=bar
share|improve this answer
Woot! I've been fighting with this for days. Thanks! – baudtack Aug 23 '11 at 17:07

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.