Basic auth Apache and multiple IP conditions

Solution 1:

Hum this is a logic problem. You did not clearly expressed what is the expected result, though. So I'm half guessing.

Assuming you want this :

  • domain=example => ok (no password)
  • address=1... => ok (no password)
  • address=8... => ok (no password)
  • anything else => not ok (password needed)

You just need to concentrate on the "ok" part. The expression that is true for all these conditions is

a=1 || a=8 || d=example

Now, you need to do nothing in this case, but for all other cases, you need to do something (ask password), so you have to reverse the test. The needed expression becomes

not (a=1 || a=8 || d=example)

Developping not (a=1 || a=8 || d=exampe) gives (not (a=1)) && (not (a=8)) && (not d=example)) which you can be simplified as a!=1 && a!=8 && d!=example

So, try this :

<If "%{REMOTE_ADDR} != '1.1.1.1' && %{REMOTE_ADDR} != '8.8.8.8' && %{REMOTE_HOST} != 'example.com'">
  AuthType Basic
  AuthName "restricted area"
  AuthUserFile /home/user/.htpasswd
  require valid-user
</If>