29 April 2013

Diverting visitors from a specific IP address to another website

I don't like certain companies or people looking me up. UK company A4e is one I most definitely don't want to be associated with, and I do not fancy them looking up my CV website- but I found visitor records suggesting that someone at the company has.  Obviously, this is a public page and doesn't contain much in the way of personal information- its purpose is to help people who have worked with me track me down, or people who get an email from me on a professional level look up more about me. What it is not for is the likes of A4e to data-mine and use my information for any of their purposes. So I set out to block any repeat visits, with a redirect to a page that says why.

This seems to be quite easy to accomplish with .htaccess.  To block someone from a specific IP address add the following to a .htaccess file: (or append it to an existing one, taking care not to delete important existing rules)

RewriteEngine On 
RewriteCond %{REMOTE_ADDR} ^195\.171\.111\.196
RewriteRule .* http://goaway.coopey.me [R,L] 

If you wanted to block a whole range of IP addresses use '.$' e.g. to block the range 195.171.111.* use the following for line two:

RewriteCond %{REMOTE_ADDR} ^195\.171\.111\.$ 

If you wanted to block a subsection, use the following format: (this will block to

RewriteCond %{REMOTE_ADDR} ^195\.171\.111\.(1[6-9]|2[0-9]|3[0-2])$

The IP address in this example seems to be A4e's fixed IP from their head office in Sheffield. The page goaway.coopey.me is hosted at Tumblr, and says 'Access Denied' with a short explanation why.

Remember, this will only work on visitors from a specific IP (or range of IPs). If you block a range, you may be blocking more people than you attended, and if you block someone who uses a dynamic IP address (their ISP changes this each time they connect to the internet) then they will not be permanently redirected- and the people who are subsequently assigned the IP will have to access!