Using two environment variables to block access in Apache .htaccess

1.5k Views Asked by At

I'm using SetEnvIf and Deny to block access to certain countries in my .htaccess.

But I need to exclude certain URLs from this blocking, and thus I'm setting another environment variable for those URLs.

How do I Deny based on a combination of variable 1 and variable 2 ?

SetEnvIf GEOIP_COUNTRY_CODE xx BlockedCountry
SetEnvIf Request_URI "^/important" NeverBlock

In pseudo code I want to do this now:

Deny from env=BlockedCountry && !NeverBlock
1

There are 1 best solutions below

3
On BEST ANSWER

From Apache documentation :

Syntax: Deny from all|host|env=[!]env-variable [host|env=[!]env-variable] ...

Which means you can combine conditions one after the other (there is no "boolean" operators in between).

So in your case, it should look like this

Deny from env=BlockedCountry env=!NeverBlock

Update

From what you said, it looks like this implies an OR condition instead of an AND (what you want). To do so, you can use this workaround

SetEnvIf GEOIP_COUNTRY_CODE xx MustBeBlocked
SetEnvIf Request_URI "^/important" !MustBeBlocked

Deny from env=MustBeBlocked

With this technique, you set/unset the environment variable depending on the case, which simulates an AND condition.