2

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 Answer 1

4
+50

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.

3
  • Thanks for the reference, but there seems to be an implied OR between the 2 conditions. Now access is denied when its a bad country OR a URL that doesn't fall in the 'never block' category. Thats too much. I need an AND for the conditions. Dec 6, 2018 at 15:52
  • This works in this short example, but is there a gurantee that Apache processes the directives in the order that they appear in the config ? I really would have preferred to use 2 variables. Dec 10, 2018 at 10:03
  • My guess would be that each line is evaluated sequentially. Maybe you can find an answer in the Apache documentation. Another solution would be to use mod_rewrite (along with mod_geoip) and in this case each condition is evaluated in order of appearance. Dec 12, 2018 at 18:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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