Simple condition, which never match and set variable:

SetEnvIf Request_URI "^/path/to/something*" access_granted

Checking URI by using PHP is giving correct string, however variable is never set.

echo getenv('Request_URI'); --> /path/to/something
echo getenv('access_granted'); --> <<empty>>

Simple as that, but still and always failing to match.

Update: interesting point is, that I can achieve access_granted=1 by string:

SetEnvIf Request_URI "^/path/*" access_granted

/path is also RewriteBase - maybe it's connected somehow, I don't know


Second argument to SetEnvIf is supposed to be regexp, so it should read as:

SetEnvIf Request_URI ^/path/to/something.* access_granted

your second attempt:

 SetEnvIf Request_URI "^/path/*" access_granted

works because indeed you have 0 or more "/" symbols following /path

In other words - you've attempted to use shell globs where regexp is expected.

  • Good point, I did not realized, that dot is missing there. However, now even 2nd attempt does not work, until I simplify regex to SetEnvIf Request_URI ^/.*$ NotificationUri . Seems, like there is something else in the URI, as appears to be. Maybe mod_rewrite is screwing things up ? – Ivan Nov 10 '15 at 22:44

The issue you are facing is maybe caused by mod_rewrite as explained under. Rewrite rules are applied before applying setenv, so that the rewritten request uri is passed to it. Try to set the env variable on rewrite.

  • 1
    This correctly explains the problem and should be the accepted answer. – dzuelke Mar 24 '16 at 23:16

Simple solution. Request_URI is not the name of the environment variable, you're setting a variable called access_granted.

  • OK, seems, that I wrote my question wrong. Sure, I'm checking access_granted also, but it's always unset, therefore I didn't mention it in code. Second line of code is only showing, that Request_URI is exactly same as I whrote to condition. – Ivan Apr 10 '14 at 15:24
  • It isn't exactly unset, it is empty because you don't assign any value to the variable... That would require something like: SetEnvIf Request_URI "^/path/to/something*" access_granted=<value> – HBruijn Apr 10 '14 at 18:36
  • I'm affrraid, =XY is not mandatory :) When is not set, apache is assigning 1 by default, according to docs.. But I tried it also, no change unfortunately. Regex simply does not match in my opinion. There must be something changing the URI, maybe mod_rewrite or I don't know. – Ivan Apr 10 '14 at 21:57
  • Maybe PassEnv access_granted? – HBruijn Apr 11 '14 at 6:29
  • Not sure if I've got your point :) access_granted is not an shell variable, so does it help anyway ? I've tried that anyway - no change unfortunately – Ivan Apr 11 '14 at 11:49

I know it's an old topic, but I came across it on my current research, maybe others will have related issues...

For me the following code worked fine and the expression was matched after rewrites!

URL: www.example.de/lockthissite/

.htaccess code:

SetEnvIfNoCase Request_URI ^/lockthissite/$ SECURED=yes

AuthType Basic 
AuthName "restricted access" 
AuthUserFile /path/to/my/.htpasswd 
Require valid-user 
Satisfy any 
Order allow,deny 
Allow from all
Deny from env=SECURED

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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