Sunday, 14 December 2014

Using .htaccess file to forbid spammers access to a URL

This is pretty straightforward, but I struggled to find a simple explanation of this and worked it out with trial and error, so thought I might as well post it here in case it helps somebody else :)

I run another website which I is hosted on Linux/Apache, and for a while I ran a forum on it. However the forum was never used, apart from spam bots, so I removed it. I later discovered that huge amounts of bandwidth was being taken up by spam bots trying to spam the now non-existent forum, each one being served a nice 404 message and taking up processing time on my site, so I decided to forbid access to it.

The forum sat at: http:////forum/index.php, and both /forum and /forum/index.php were getting lots of traffic. 

To block this I edited the Apache .htaccess file, and added this line:
RewriteRule ^forum($|/) - [F]

This is a rewrite rule, for any path after /forum, that results in the following "403" forbidden message:

You don't have permission to access /forum on this server.

This is exceptionally lightweight and does not trigger a 404. Problem solved!

If you want do a simple redirect, perhaps becuase you have changed your structure, this is even more simple.  Just use teh "Redirect 301" command as per the example below:

Redirect 301 /top-tens/bestselling-electronics/bestselling-televisions/bestselling-sony-televisions/
Redirect 301 /top-ten/watches/omega/ladies

Please note that the "from" URL must be a relative URL, whilst the "to" URL is the full URL including domain.

Carissima Gold 9 ct Yellow Gold Two-Row Diamond Cut Curb Bracelet of 21 cm 8.5-inch on
Carissima Gold 9 ct Yellow Gold Two-Row Diamond Cut Curb Bracelet of 21 cm/8.5-inch