Forum Moderators: phranque

Message Too Old, No Replies

htaccess question

         

SirTalksalot

9:07 pm on Jan 3, 2007 (gmt 0)

10+ Year Member



Hi all, I'm trying to redirect any traffic that comes to my site from one other specific site. I'm using the code which has been suggested to me


#RewriteEngine on
#RewriteCond %{HTTP_HOST} ^mysite.co.uk$ [OR]
#RewriteCond %{HTTP_HOST} ^mysite.co.uk$
#RewriteRule ^(.*)$ public_html/mysite/$1 [R=301,L]

#RewriteCond %{HTTP_REFERER} ^examplesite.com*
#RewriteRule ^/$ redirectsite.com [R]

The last two lines are the part I'm unsure about. The first part was already in my htaccess file, I don't know if it's affecting the latter in any way. I know nothing about htaccess and the like. If someone could spell out what I'm doing wrong I'd really appreciate the help.

Thank you in advance.

jdMorgan

9:54 pm on Jan 3, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Several minor problems in there. I'd suggest:

RewriteCond %{HTTP_REFERER} ^http://(www\.)?example\.com
RewriteRule .* http://redirectsite.com/ [R=302,L]

The most critical problem was that the code was apparently intended for use in httpd.conf or another server-level config file. In a per-directory .htaccess context, the path to the current directory is stripped, and therefore URL-paths 'seen' by RewriteRule will not have a leading "/" on them. Therefore, your pattern would never match a normal request, and the rule would never be invoked.

I'd also suggest reversing the rules -- There's no use doing a domain canonicalization redirect if you're going to pass the request to another site anyway.

I assume that you want to redirect *all* URL requests referred from example.com to redirectsite.com, so I have changed the RewriteRule pattern accordingly.

Unless you actually need to deliver desirable traffic to redirectsite, consider, that it is bad practice to pass off problems to another site. If you simply want to dump the traffic from example.com, change the RewriteRule to:


RewriteRule .* - [F]

That will simply return a 403-Forbidden response and be done with it, rather than tying up internet bandwidth and some other site's server dealing with a troublemaker. Be aware that when other sites dump unwanted traffic on my site, I will go after the redirecting site for theft of services...

I also assume that in your actual code, the domains given in your first two RewriteConds are not really identical. If so, you can delete the first (redundant) RewriteCond.

For more information, 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

SirTalksalot

10:12 pm on Jan 3, 2007 (gmt 0)

10+ Year Member



Thank you, that was amazingly helpful. I really appreciate it. :)