Welcome to WebmasterWorld Guest from 100.26.176.182

Forum Moderators: Ocean10000 & phranque

Message Too Old, No Replies

conditional redirect

     
9:37 pm on Oct 25, 2016 (gmt 0)

Junior Member

5+ Year Member Top Contributors Of The Month

joined:Sept 13, 2013
posts: 193
votes: 1


Wonder if someone can help me.

I have a redirect, based on {HTTP_REFERRER}, but I want to allow one particular IP, coming from that REFERRER to NOT be redirected. How do I do this?
4:58 am on Oct 26, 2016 (gmt 0)

Administrator

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Aug 10, 2004
posts:11884
votes: 250


You need 2 RewriteCond directives before the RewriteRule that match the referrer and not match the IP.
11:17 am on Oct 26, 2016 (gmt 0)

Junior Member

5+ Year Member Top Contributors Of The Month

joined:Sept 13, 2013
posts: 193
votes: 1


Well, if I have
RewriteCond {HTTP_REFERRER} referrer [AND]
then I need a
RewriteCond {REMOTE_ADDR} not ^11\.22\.33\.44
RewriteRule thendothis

How do I do that "not"?
3:15 pm on Oct 26, 2016 (gmt 0)

Administrator

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Aug 10, 2004
posts:11884
votes: 250


http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewritecond

...there is additional syntax available to perform other useful tests against the Teststring:

1. You can prefix the pattern string with a '!' character (exclamation mark) to negate the result of the condition, no matter what kind of CondPattern is used.

2. You can perform lexicographical string comparisons:
...
=CondPattern
Lexicographically equal
Treats the CondPattern as a plain string and compares it lexicographically to TestString. True if TestString is lexicographically equal to CondPattern (the two strings are exactly equal, character for character). If CondPattern is "" (two quotation marks) this compares TestString to the empty string.
3:28 pm on Oct 26, 2016 (gmt 0)

Junior Member

5+ Year Member Top Contributors Of The Month

joined:Sept 13, 2013
posts: 193
votes: 1


So then I want this?

RewriteCond {HTTP_REFERRER} referrer [AND]
RewriteCond {REMOTE_ADDR} !^11\.22\.33\.44
RewriteRule thendothis
10:49 pm on Oct 26, 2016 (gmt 0)

Administrator from US 

WebmasterWorld Administrator not2easy is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Dec 27, 2006
posts:4595
votes: 375


I am pretty sure you would want to add a "$" anchor to the end of this line:
RewriteCond {REMOTE_ADDR} !^11\.22\.33\.44
thus:
RewriteCond {REMOTE_ADDR} !^11\.22\.33\.44$
10:53 pm on Oct 26, 2016 (gmt 0)

Junior Member

5+ Year Member Top Contributors Of The Month

joined:Sept 13, 2013
posts: 193
votes: 1


Nice. Thank you! I'll test it out.
6:15 am on Oct 27, 2016 (gmt 0)

Administrator

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Aug 10, 2004
posts:11884
votes: 250


"Lexicographically equal treats the CondPattern as a plain string"

You don't really need a regular expression.
I would go with this:

RewriteCond {REMOTE_ADDR} !=11.22.33.44
6:02 pm on Oct 27, 2016 (gmt 0)

Junior Member

5+ Year Member Top Contributors Of The Month

joined:Sept 13, 2013
posts: 193
votes: 1


Nope. Didn't work. I get a 500 error, whether or not that second RewriteCond URL has a $ at the end of it.

Let me be specific. What didn't work was

RewriteCond {HTTP_REFERER} thisandthat [NC,AND]
RewriteCond {REMOTE_ADDR} !=111.222.333.444
RewriteRule ....
9:59 pm on Oct 28, 2016 (gmt 0)

Administrator

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Aug 10, 2004
posts:11884
votes: 250


Is there a corresponding message in the server error log?
10:27 pm on Oct 28, 2016 (gmt 0)

Junior Member

5+ Year Member Top Contributors Of The Month

joined:Sept 13, 2013
posts: 193
votes: 1


I think we're looking at this error

[Thu Oct 27 13:02:03.879464 2016] [core:alert] [pid 93110] [client 111.222.333.444:48082] /Users/me/.htaccess: RewriteCond: unknown flag 'AND', referer: http://thisandthat.com?pid=9300

Is there something wrong with my "AND?
12:12 am on Oct 29, 2016 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member keyplyr is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Sept 26, 2001
posts:12913
votes: 893


Remove the AND
12:23 am on Oct 29, 2016 (gmt 0)

Junior Member

5+ Year Member Top Contributors Of The Month

joined:Sept 13, 2013
posts: 193
votes: 1


I don't understand. If I have a string of RewriteConds that are to be obeyed individually, I need them separated by an "OR". Is "AND" a default if the is no "OR"? That is, are two successive RewriteCond statements without an "OR" obeyed logically as an "AND"?
12:36 am on Oct 29, 2016 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member keyplyr is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Sept 26, 2001
posts:12913
votes: 893


They are different conditions. One is referer and one is an address... so no you don't want to connect them. Remove the superfluous attributes as well.

RewriteEngine On
RewriteCond {HTTP_REFERER} thisandthat
RewriteCond {REMOTE_ADDR} !^111.222.333.444
RewriteRule ....

Also, if that referrer has any special characters, you need to escape them. Best to use just the domain name and not include any parameters.
1:05 am on Oct 29, 2016 (gmt 0)

Junior Member

5+ Year Member Top Contributors Of The Month

joined:Sept 13, 2013
posts: 193
votes: 1


Ah, so if I have a list of REFERERS that I all want the RewriteRule to apply to, I'd use an "OR" between them. But RewriteCond {REMOTE_ADDR} !^111.222.333.444 is "AND"ed with the lot of them. As in

RewriteEngine On
RewriteCond {HTTP_REFERER} thisandthat [OR]
RewriteCond {HTTP_REFERER} morestuff
RewriteCond {REMOTE_ADDR} !^111.222.333.444
RewriteRule ....

means that either one REFERER OR the other will be evaluated for the RewriteRule with a "NOT" of the specified address? Do I have that right? That seems syntactically devious.
1:07 am on Oct 29, 2016 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member keyplyr is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Sept 26, 2001
posts:12913
votes: 893


Yes, that's it
1:15 am on Oct 29, 2016 (gmt 0)

Junior Member

5+ Year Member Top Contributors Of The Month

joined:Sept 13, 2013
posts: 193
votes: 1


Thank you! That's syntactically devious, but once you understand it, it's quite marvelous.
3:28 pm on Oct 29, 2016 (gmt 0)

Junior Member

5+ Year Member Top Contributors Of The Month

joined:Sept 13, 2013
posts: 193
votes: 1


Um, but it doesn't work. When I put

RewriteEngine On
RewriteCond {HTTP_REFERER} thisandthat
RewriteCond {REMOTE_ADDR} !^111.222.333.444
RewriteRule ....

in my .htaccess file, and I come in from referer "thisandthat" using IP 111.222.333.444, the RewriteRule is executed. Exactly what happens when I come in using any other IP.
7:20 pm on Oct 29, 2016 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member keyplyr is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Sept 26, 2001
posts:12913
votes: 893


Exactly what happens when I come in using any other IP
Be more explicit. What happens? And what is your last line exactly?

Just to be sure, this is what your code should look like:

RewriteEngine On
RewriteCond %{HTTP_REFERER} thisandthat
RewriteCond %{REMOTE_ADDR} !^111\.222\.2333\.444
RewriteRule - [F]
(with the escapes mentioned earlier)

Of course your rule may be different.
7:58 pm on Oct 29, 2016 (gmt 0)

Junior Member

5+ Year Member Top Contributors Of The Month

joined:Sept 13, 2013
posts: 193
votes: 1


Sorry. This is my complete listing

RewriteEngine On
RewriteCond {HTTP_REFERER} thisandthat
RewriteCond {REMOTE_ADDR} !^111.222.333.444
RewriteRule ^onefile\.htm anotherfile.htm [R=302,L]

Without that REMOTE_ADDR RewriteCond, everyone coming from REFERER thisandthat who tries to get to onefile.htm, will get sent to anotherfile.htm. That works great.

With that REMOTE_ADDR RewriteCond, everyone trying to get to onefile.htm is still sent to anotherfile.htm. Including someone with IP 111.222.333.444. But what I'm trying to do is make that IP an exception to the rule.
8:29 pm on Oct 29, 2016 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member keyplyr is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Sept 26, 2001
posts:12913
votes: 893


OK, without that last line I was not understanding what you were trying to accomplish.

Is it absolutely necessary that this IP is exempted from the redirect? Sometimes keeping things simple is the best approach.

[edited by: keyplyr at 9:05 pm (utc) on Oct 29, 2016]

8:43 pm on Oct 29, 2016 (gmt 0)

Junior Member

5+ Year Member Top Contributors Of The Month

joined:Sept 13, 2013
posts: 193
votes: 1


My apologies. What you have listed is what is in my .htaccess file. I stupidly "abbreviated" the second clause in my RewriteRule, removing the http: part. So no characters were removed automatically.
7:20 am on Oct 31, 2016 (gmt 0)

Administrator

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Aug 10, 2004
posts:11884
votes: 250


!< Is a negated lexicographic comparison.
Plain text TestString, no escapes necessary.
However you probably want to use != in your application.
12:58 pm on Oct 31, 2016 (gmt 0)

Junior Member

5+ Year Member Top Contributors Of The Month

joined:Sept 13, 2013
posts: 193
votes: 1


Is it absolutely necessary that this IP is exempted from the redirect? Sometimes keeping things simple is the best approach.


Well, if I didn't think it was absolutely necessary, I wouldn't be here. "Keeping things simple" might well remove half of this discussion forum! I like to believe that not keeping things simple is what makes the world go 'round.
10:03 am on Nov 1, 2016 (gmt 0)

Administrator

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Aug 10, 2004
posts:11884
votes: 250


I have a redirect, based on {HTTP_REFERRER}, but I want to allow one particular IP, coming from that REFERRER to NOT be redirected.


In other words , if the referrer is this and the IP is not equal that, redirect the request?

!=
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members