Forum Moderators: phranque

Message Too Old, No Replies

Redirect Based On Referrer

         

Neo541

5:11 pm on Dec 21, 2005 (gmt 0)

10+ Year Member



We sell a product, and occasionally someone will try to sell a knock off on Ebay, always linking back to our site for more information on that product.

What I would like to do is to redirect anyone who has an ebay referrer to a page where I explain how to tell if the product is real or a knock off, etc.

So, I need a redirect that will redirect anyone referred by any page on Ebay [cgi.ebay.com...] to one specific page, regardless of the page on my site they are linking to. Make sense?

Here's what I have so far, based on changing code I found for an image redirect. I'm sure the code is brutal, so can anyone help make code that would, you know, work?

RewriteEngine On
RewriteCond %{HTTP_REFERER} ^http://(cgi\.)?ebay\.com/ [nc]
RewriteRule .*\.(htm¦html¦)$ [mysite.com...] [nc]

[edited by: jdMorgan at 7:22 pm (utc) on Dec. 21, 2005]
[edit reason] Fixed smiley in code. [/edit]

jdMorgan

2:21 am on Dec 22, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'd use:

RewriteEngine on
RewriteCond %{HTTP_REFERER} ^http://(cgi\.)?ebay\.com [NC]
RewriteRule \.html?$ /newpage.htm [NC,L]

Jim

Neo541

2:59 pm on Dec 30, 2005 (gmt 0)

10+ Year Member



Thanks for the response JD.

I tried the code below:

RewriteEngine on
RewriteCond %{HTTP_REFERER} ^http://(cgi\.)?ebay\.com [NC]
RewriteRule \.html?$ /ebay.html [NC,L]

and it gave me a 500 server error.

Any ideas?

jdMorgan

3:52 pm on Dec 30, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Do you have any other working mod_rewrite code?

If not, then try adding:


Options +FollowSymLinks

ahead of RewriteEngine on

If you already have other working RewriteRules, then I'd suggest you look at your raw server error log -- It will often tell you exactly what the problem is, including the problem fix above.

Jim

Neo541

4:57 pm on Dec 30, 2005 (gmt 0)

10+ Year Member



I have other working mod_rewrite code in .htaccess, and do have Options +FollowSymLinks listed above the mod_rewrite I just attempted.

Looked in my logs, it says:

"mod_rewrite: maximum number of internal redirects reached. Assuming configuration error. Use 'RewriteOptions MaxRedirects' to increase the limit if neccessary."

Not sure exactly what that means.

jdMorgan

5:06 pm on Dec 30, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sorry, it's looping... You'll need to add an exclusion to prevent /ebay.html from being rewritten to itself:

RewriteEngine on
RewriteCond %{REQUEST_URI} !^/ebay\.html$
RewriteCond %{HTTP_REFERER} ^http://(cgi\.)?ebay\.com [NC]
RewriteRule \.html?$ /ebay.html [NC,L]

Jim

Neo541

5:16 pm on Dec 30, 2005 (gmt 0)

10+ Year Member



Works perfect, thanks for your help Jim!

Pfui

12:03 am on Jan 2, 2006 (gmt 0)

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



Along the same lines --

Is it possible to automatically redirect anyone viewing one of my sites' cached or e-mailed pages on Google's or Yahoo's servers, for example, to its original page? (The preceding eBay example redirects all requests to one page.)

I'm interested in redirecting people (& bots & crawlers) accessing my HTML content on other servers because the images on each page are hotlinked-blocked and I'm ending up with a gazillion 403s in my logs every time someone looks at a page that's somewhere else (including in their local cache). I'm even more keenly interested because my 'seen elsewhere' content has been used in ways my local mod_rewrites prevent.

Alas, I'm unable to test what I'm trying to do on a live site with real off-site referrers. So before I run the risk of
loops within my 90k .htaccess file and/or treating visitors to Server Errors galore, here are three sets of examples, LONG, MEDIUM and SHORT.

What do you think, please?

1.) LONG: Does the following set look somewhat passable?

RewriteEngine on 
## Google
RewriteCond %{HTTP_REFERER} google\.*$ [NC,OR]
RewriteCond %{HTTP_REFERER} 64\.233\.161\.104/.*$ [NC,OR]
RewriteCond %{HTTP_REFERER} 66\.249\.93\.104/.*$ [NC,OR]
RewriteCond %{HTTP_REFERER} search\?q=cache [NC,OR]
## Yahoo
RewriteCond %{HTTP_REFERER} yahoo\.*$ [NC,OR]
RewriteCond %{HTTP_REFERER} 66\.94\.231\.568/.*$ [NC,OR]
RewriteCond %{HTTP_REFERER} ShowLetter\?$ [NC,OR]
## Other
RewriteCond %{HTTP_REFERER} netscape\.*$ [NC,OR]
RewriteCond %{HTTP_REFERER} earthlink\.*$ [NC,OR]
## Local
RewriteCond %{HTTP_REFERER} [127\.0\.0\.1...] [NC]
RewriteRule ^.*$ http://www.example.com/$1 [NC,L]

(The referring URLs have LOADS of variables after question marks but the suffixes seem more consistent than not but for countries: google.ca, google.de, etc. Alas, the prefixes, particularly yahoo's, are more complicated.)

2.) MEDIUM: Alternatively, this next set is simpler and isn't mired in umpteen IP and HOST possibilities --

RewriteEngine on 
RewriteCond %{REQUEST_URI} ^(cache¦Inbox¦mail¦Show¦ShowLetter¦user¦webmail)$ [NC,OR]
RewriteCond %{HTTP_REFERER} [127\.0\.0\.1...] [NC]
RewriteRule ^.*$ http://www.example.com/$1 [NC,L]

3. SHORT: Finally, down to basics, and minus the 127.0.0.1 idea. Too succinct or simply doable? Used all on one line:

RewriteEngine on 
RewriteRule \(cache¦Inbox¦mail¦Show¦ShowLetter¦user¦webmail)$ http://www.example.com/$1 [NC,L]

Thanks in advance for your help!

jdMorgan

1:41 am on Jan 2, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



1) I'm not sure I understand your transition from HTTP_REFERER to REQUEST_URI between the first and last two examples. The referrer info won't be present in REQUEST_URI... I may be missing something here, though.

2) The first two variations and a correction of the third will require the expresson on the left side of the rewriterule to be parenthesized to create the $1 back-reference.

3) Dump trailing ".*$" sequences. They do nothing but slow down your server, and offer no advantage over a non-end-anchored pattern unless a back-reference is being created. In most cases, this is a dead give-away for auto-generated code.

Because this is an 'optional' rather than 'critical' application, I'd lean more toward something compact like version two:


RewriteEngine on
RewriteCond %{HTTP_REFERER} [?&](cache¦Inbox¦mail¦Show¦ShowLetter¦user¦webmail)= [NC,OR]
RewriteCond %{HTTP_REFERER} http://127\.0\.0\.1/ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

I'm assuming those referrer parameters all have "=" after them, so I added that to make them as specific as possible (given the already-compressed nature of the pattern).

Jim

jdMorgan

2:33 am on Jan 2, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I thought about this for awhile, and it's not going to work. The problem is that only the images will be fetched from your server; The page itself will be served from the cache. So, your server won't get a chance to redirect the page itself, and so the proposed solution won't be effective.

What you'll need is a client-side solution, such as a short JavaScript meta-refresh based on the current page location.

Jim