Forum Moderators: phranque

Message Too Old, No Replies

conditional redirect

         

Dan99

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

10+ Year Member Top Contributors Of The Month



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?

phranque

4:58 am on Oct 26, 2016 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



You need 2 RewriteCond directives before the RewriteRule that match the referrer and not match the IP.

Dan99

11:17 am on Oct 26, 2016 (gmt 0)

10+ Year Member Top Contributors Of The Month



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"?

phranque

3:15 pm on Oct 26, 2016 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



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.

Dan99

3:28 pm on Oct 26, 2016 (gmt 0)

10+ Year Member Top Contributors Of The Month



So then I want this?

RewriteCond {HTTP_REFERRER} referrer [AND]
RewriteCond {REMOTE_ADDR} !^11\.22\.33\.44
RewriteRule thendothis

not2easy

10:49 pm on Oct 26, 2016 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



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$

Dan99

10:53 pm on Oct 26, 2016 (gmt 0)

10+ Year Member Top Contributors Of The Month



Nice. Thank you! I'll test it out.

phranque

6:15 am on Oct 27, 2016 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



"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

Dan99

6:02 pm on Oct 27, 2016 (gmt 0)

10+ Year Member Top Contributors Of The Month



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 ....

phranque

9:59 pm on Oct 28, 2016 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



Is there a corresponding message in the server error log?

Dan99

10:27 pm on Oct 28, 2016 (gmt 0)

10+ Year Member Top Contributors Of The Month



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?

keyplyr

12:12 am on Oct 29, 2016 (gmt 0)

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



Remove the AND

Dan99

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

10+ Year Member Top Contributors Of The Month



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"?

keyplyr

12:36 am on Oct 29, 2016 (gmt 0)

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



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.

Dan99

1:05 am on Oct 29, 2016 (gmt 0)

10+ Year Member Top Contributors Of The Month



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.

keyplyr

1:07 am on Oct 29, 2016 (gmt 0)

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



Yes, that's it

Dan99

1:15 am on Oct 29, 2016 (gmt 0)

10+ Year Member Top Contributors Of The Month



Thank you! That's syntactically devious, but once you understand it, it's quite marvelous.

Dan99

3:28 pm on Oct 29, 2016 (gmt 0)

10+ Year Member Top Contributors Of The Month



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.

keyplyr

7:20 pm on Oct 29, 2016 (gmt 0)

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



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.

Dan99

7:58 pm on Oct 29, 2016 (gmt 0)

10+ Year Member Top Contributors Of The Month



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.

keyplyr

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

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



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]

Dan99

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

10+ Year Member Top Contributors Of The Month



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.

phranque

7:20 am on Oct 31, 2016 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



!< Is a negated lexicographic comparison.
Plain text TestString, no escapes necessary.
However you probably want to use != in your application.

Dan99

12:58 pm on Oct 31, 2016 (gmt 0)

10+ Year Member Top Contributors Of The Month



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.

phranque

10:03 am on Nov 1, 2016 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



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?

!=