Forum Moderators: phranque

Message Too Old, No Replies

302 Redirect, but not for all incoming referrers

I don't want users coming from some domain to be referred.

         

forzatio

6:19 pm on Oct 22, 2006 (gmt 0)

10+ Year Member



Hello, today I made a 302 redirect in .htaccess to another domain.
The 302 to the page is actually an exact mirror of the source page.
For bandwidth reasons I don't want to refer everyone to the new page.
I know that from one domain I get a lot of traffic, I don't want users coming from that domain to go to my new page.

How can I prefend users to be referred in this case? I need to disable the 302 redirect for users of that referrer domain only.
I want to do this in .htaccess but it's not supporting apache, so it should be "basic text for it" I guess. thank you.

forzatio

4:11 pm on Oct 24, 2006 (gmt 0)

10+ Year Member



Hello I didn't get a reply to this but really it should be possible to exclude some users to follow the 302 redirect right?
Users coming from 1 domain must not follow the 302 redirect.

I hope someone can help me with this one.

jdMorgan

4:24 pm on Oct 24, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Please explain "not supporting apache":

I want to do this in .htaccess but it's not supporting apache, so it should be "basic text for it" I guess.

The solution is to use RewriteCond to test the referrer variable %{HTTP_REFERER}, and then use a RewriteRule to do the redirect only if the result of that test indicates that the referrer is NOT a particular site.

This is very similar to common anti-hotlinking code, where visitors referred from hotlinking pages are redirected to an alternate image.

See the documents cited in our forum charter [webmasterworld.com] and the tutorials in the Apache forum section of the WebmasterWorld library [webmasterworld.com].

Jim

forzatio

10:08 pm on Oct 24, 2006 (gmt 0)

10+ Year Member



Well I got it after doing some two hour research.
What I want is actually below and it seem to work, but is this optimized?

#Starting mod_rewrite...
RewriteEngine on

#The referer URL's that are allowed to link files.
#The third (no referer) represents a link typed in.
RewriteCond %{HTTP_REFERER}!^http://(www\.)?allowed-domain.com/.*$ [NC]
RewriteCond %{HTTP_REFERER}!^http://(www\.)?allowed-domain.com.*$ [NC]
RewriteCond %{HTTP_REFERER}!^$

#The file that will returned to all but the above allowed referers.
RewriteRule \.(html)$ [new-website.com...] [R=302,L]

jdMorgan

11:28 pm on Oct 24, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'd recommend a few tweaks:

RewriteCond %{HTTP_REFERER} !^http://(www\.)?allowed-domain\.com [NC]
RewriteCond %{HTTP_REFERER} .
RewriteRule ^(.*\.html)$ http://www.new-website.com/$1 [R=302,L]

Jim

[edited by: jdMorgan at 11:29 pm (utc) on Oct. 24, 2006]

forzatio

9:13 am on Oct 25, 2006 (gmt 0)

10+ Year Member



Hello JDmorgan,

your simplified script does follow the 302 redirect for urls like [allowed-domain.com...] those are redirected to www.new-website.com but they shouldn't.

I actually want all referrers from allowed-domain.com including subdomains not to follow the 302 redirect, with your script this does not work?

I can use my own script (it works) but I have another issue with that, if there is no referrer it doesn't follow the 302 redirect.

I want direct typein traffic to be allowed = no referrer, but I do want to make Google follow the redirect, the problem is the googlebot doesn't have a referrer aswell.

forzatio

5:10 pm on Oct 25, 2006 (gmt 0)

10+ Year Member



I tried a lot of things with HTTP_HOST , I want direct type-in traffic and allowed-domain.com not to follow the 302 redirect.

However the direct type-in traffic DOES follow the 302 redirect.
I know I could use!^$ to allow non-referer traffic, but then the search engine bots won't follow the 302.

I just can't get it to work, I must allow searchengine bots to follow the 302, but at the same time I have to disable the 302redirect for direct type-in traffic. (both are non-referer traffic!)

Hope I could get this to work finally?

#Starting mod_rewrite...
RewriteEngine on

#The referer URL's that are allowed to link files.
#The third (no referer) represents a link typed in.

RewriteCond %{HTTP_HOST}!^sub\.website\.com/.*$ [NC]
RewriteCond %{HTTP_REFERER}!^http://(www\.)?allowed-domain.com/.*$ [NC]
RewriteCond %{HTTP_REFERER}!^http://(www\.)?allowed-domain.com.*$ [NC]

#The file that will returned to all but the above allowed referers.
RewriteRule \.(html)$ [new-website.com...] [R=302,L]

jdMorgan

4:49 am on Oct 26, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Again, the following should be entirely equivalent:

# Starting mod_rewrite...
RewriteEngine on
#
# The referer URL's that are allowed to link files.
RewriteCond %{HTTP_HOST} !^sub\.website\.com [NC]
RewriteCond %{HTTP_REFERER} !^http://(www\.)?allowed-domain\.com [NC]
# The file that will returned to all but the above allowed referers.
RewriteRule \.html$ http://www.new-website.com/ [R=302,L]

(A pattern which is not back-referenced and which starts with "^.*" or ends with ".*$" is almost always an utter waste of CPU time, and can be omitted. In this case, it allows you to remove an RewriteCond entire line.)

Note that your original code (and the code above) will redirect any request for any page to the 'home page' of new-website.com -- no requested-page information is being passed-through.

If you wish to preserve the requested URL-path, then change the rule:


RewriteRule ^(.+\.html)$ http://www.new-website.com/$1 [R=302,L]

Jim

forzatio

8:00 am on Oct 26, 2006 (gmt 0)

10+ Year Member



Hey JdMorgan, I tried your code but it still refers all type-in traffic.
(My .htaccess is placed in the subdomain root of the domain, which is under my control, the full domain is not under my control)
I could use your better writing techniques though, so I could skip some rules out of it.

Anyway this code below seems to do what I want, allowed-site.com and direct type-in traffic is not refered but the searchengine bots are following the 302 like I want.

RewriteEngine on

RewriteCond %{HTTP_REFERER}!^http://(www\.)?allowed-site\.com [NC]
RewriteCond %{HTTP_REFERER}!^$
RewriteRule \.html$ [new-website.com...] [R=302]

RewriteCond %{HTTP_USER_AGENT} googlebot [NC,OR]
RewriteCond %{HTTP_USER_AGENT} Slurp [NC,OR]
RewriteCond %{HTTP_USER_AGENT} msnbot
RewriteRule \.html$ [new-website.com...] [R=302,L]