Forum Moderators: phranque

Message Too Old, No Replies

RewriteCond [OR] Ignored

         

Jim123

12:31 am on Jan 26, 2010 (gmt 0)

10+ Year Member



I'm trying to refer all traffic via htaccess to a subscription page except certain IP addresses. I wanted to do it with:

# REDIRECT EVERYONE TO SUBSCRIPTION FORM EXCEPT
RewriteCond %{REMOTE_ADDR} !^12\.34\.56\.78$ [OR]
RewriteCond %{REMOTE_ADDR} !^87\.65\.43\.21$
RewriteRule .* [domainname.com...]

I read the above as if visiter is NOT IP address 1 OR NOT IP address 2 refer to subscription page on another host.

When I use this code all visitors are refered to the subscription page, including the two IP addresses.

When I use 1 IP address like this

# REDIRECT EVERYONE TO SUBSCRIPTION FORM EXCEPT
RewriteCond %{REMOTE_ADDR} !^12\.34\.56\.78$
RewriteRule .* [domainname.com...]

this IP address goes to the index page of the website. When I remove the ! it will go to the sub page. This code works.

So, in my eyes the [OR] part of the code is not working.

Is there a way to make this code work? Where is my mistake?
Is there a better/another way doing this? If yes, an example please, because I am a beginner.

Thanks
Jim

jdMorgan

12:59 am on Jan 26, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Logic error:

Take the example Remote IP address 12.34.56.78
This matches the first rewritecond pattern, so that condition does not match (it is negated by "!")
But it does not match 87.65.43.21, so thre second RewriteCond does match, and the 302 redirect is taken.

Now reverse the test and try 87.65.43.21 first.

In fact an IP address cannot be NOT 1 OR NOT 2 at the same time, it will either match one negated condition or the other, or both.

Take the [OR] off your negative-match RewriteConds to get the default logical AND function. You want to redirect if the incoming address is NOT one and NOT the other.

"If it's NOT my IP address, AND it's NOT my mate's IP address, then send them off to somewhere else."

Jim

Jim123

1:13 am on Jan 26, 2010 (gmt 0)

10+ Year Member



So simple.... Thanks

g1smd

1:18 am on Jan 26, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



(NOT A) OR (NOT B) didn't work, it has to be (NOT A) AND (NOT B).

However, NOT (A OR B) would work:

RewriteCond %{REMOTE_ADDR} !^(12\.34\.56\.78¦87\.65\.43\.21)$

Jim123

1:28 am on Jan 26, 2010 (gmt 0)

10+ Year Member



Thanks to the both of you for all your efforts!