Forum Moderators: phranque

Message Too Old, No Replies

htacces different 404 for several referer domains

         

pandorasan

8:36 pm on Jul 18, 2007 (gmt 0)

10+ Year Member



Hi, I am searching for a way to create a rule in my htacces so that I can redirect traffic to more than 1 404 page, depending from which referer they generate a 404 error. Is this possible in .htaccess.
Thanks in advance.

jdMorgan

11:46 pm on Jul 18, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



One way to do this is to declare a "fake" ErrorDocument for 404 errors, and then use mod_rewrite to rewrite requests for that fake 404 error document to either the "special" 404 error document, or to the "normal" 404 error document, based on the requesting HTTP_REFERER.

Be aware however, that it is up to the client, his/her security software, and any intervening corporate, ISP, and/or network caching proxies whether an HTTP_REFERER header is sent. So the solution must be designed to behave correctly in the even that the HTTP referrer is blank.

Jim

pandorasan

9:23 am on Jul 19, 2007 (gmt 0)

10+ Year Member



Thanks jdMorgan.
Would that be something like:

RewriteEngine on

RewriteCond %{HTTP_REFERER} ^http(s)?://(www\.)?domain1.com(/)?.*$ [NC,OR]
RewriteCond %{HTTP_REFERER} ^http(s)?://(www\.)?domain2.com(/)?.*$ [NC]

ErrorDocument 404 [mydomain.com...]
RewriteRule .* [mydomain.com...] [R]

this would do a rewrite to fake404.html when the 404 comes from domain1.com or domain2.com
if not it will go to real404.html

Is this what you mean, in the right syntax?
I am a rookie so I need some help on the correct syntax.
Thanks a lot.

g1smd

12:18 pm on Jul 19, 2007 (gmt 0)

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



Firstly, never specify a domain in a 404 ErrorDocument directive. It will return a 302 response if you do that.

.

Check exactly what jd said, word for word, then follow the instructions in that order.

.

>> declare a "fake" ErrorDocument for 404 errors <<

ErrorDocument 404 /idonotexist.html

.

>> use mod_rewrite to rewrite requests for that fake 404 error document to either the "special" 404 error document <<

RewriteCond %{HTTP_REFERER} ^http://(.+\.)?someothersite\.com [NC]
RewriteRule idonotexist.html /errors/errorpage1.html [L]

RewriteCond %{HTTP_REFERER} ^http://(.+\.)?thatothersite\.net [NC]
RewriteRule idonotexist.html /errors/errorpage2.html [L]

.

>> or to the "normal" 404 error document, based on the requesting HTTP_REFERER <<

RewriteRule idonotexist.html /errors/errorpagemain.html [L]

.

The above is rough untested code and does NOT explicitly cater for blank referrers.

I may have missed other things too. It is given as a starting experimental point for you.

You will learn a lot more fiddling with this code yourself; sometimes seemingly endless hours of frustration until you suddenly realise you overlooked something basic.

.

I expect that jd has a better way of tackling this than my example.

pandorasan

12:32 am on Jul 20, 2007 (gmt 0)

10+ Year Member



Thanks g1smd, I think slowly I am getting the logica on this one. I must admit I am not sure at all how to make the code at at this moment but the idea behind it is slowly getting clear.
But if JD or somebody else has some more specific hints for me or some waterproof code example I would really appreciate it, because this is really new to me.
Thanks a lot.

g1smd

8:40 pm on Jul 20, 2007 (gmt 0)

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



That code might work "as is".

Try it and see what you get.