Beware that editing the .htaccess file is not for the faint of heart, and we recommend not editing the file unless you know what your doing.

Today I solved a “referal spam” problem that one of my clients was having. The problem had been occuring for the past few weeks.

My first solution was to go through and block any referals from sites that had various words in their URLS relating to “casino”, “pharmaceuticals”, “mortgage”, “insurance” etc…

“”For example:

RewriteEngine On RewriteCond %{HTTP_REFERER} ^(http://www.)[a-z]+-[a-z]+- [NC] RewriteRule ^(.*) http://%{REMOTE_ADDR}/ [R=301,L] RewriteCond %{HTTP_REFERER} ^http://(www.)?.*(-|.)?insurance(-|.).*$ [OR] RewriteCond %{HTTP_REFERER} ^http://(www.)?.*(-|.)?equity(-|.).*$ [OR] RewriteCond %{HTTP_REFERER} ^http://(www.)?.*(-|.)?poker(-|.).*$ [OR] RewriteCond %{HTTP_REFERER} ^http://(www.)?.*(-|.)?casino(-|.).*$ [OR] RewriteCond %{HTTP_REFERER} ^http://(www.)?insurance.*$ [OR] RewriteCond %{HTTP_REFERER} ^http://(www.)?slamhost.*$ [OR] RewriteCond %{HTTP_REFERER} ^http://(www.)?spielepsychatrie.*$ [OR] RewriteCond %{HTTP_REFERER} ^http://(www.)?superface.*$ [OR] RewriteCond %{HTTP_REFERER} ^http://(www.)?texasholdem.*$ [OR] RewriteRule .* – [F,L] By the time I had finished, the .htaccess file was so large that the site couldn’t even load. The other reason that fix would not work is becuase the site was a mortgage website with valuable content. So naturally, there are some quality mortgage and insurance sites which have valuable links pointing to the site, and for way too many seo reasons to list we can’t go blocking those partners.

Then the obvious finnally dawned on me. Why not check the ipaddress of the reffers. If there was any pattern in the IP address, then that would make the blocking much easier and efficient.

I looked at the refferers IPs , and voila! Of over 200 reffers, only 3 different IP ranges.

I was able to remedy the problem by adding the below code to the .htaccess file:

order deny,allow
deny from xxx.xxx.xxx.xxx
allow from all
Here’s how it works:

xxx.xxx.xxx.xxx is the ip address or ip address range that you want to block.

To Block an entire range, you would omit the digits in the block. (Don’t think about that too hard, check out the below exapmple)

So:

to block reffers from: 123.456.78.9

order deny,allow
deny from 123.456.78.9
allow from all

Pretty straight forward… Next we look at blocking a range (it uses less code, which uses less overhead when the web page loads)

Let’s say you have 3 referers from IP addresses 123.456.78.1, 123.456.78.2, 123.456.78.3

To block these referers from the three IP ranges we’d use the code:

order deny,allow
deny from 123.456.78
allow from all

 

Notice how everything after the 78 is blank. You are now blocking any refferers with an IP address that starts with 123.456.78

You could also use deny from 123.456 but that might block other referers who we don’t want to ban from the site.

I wouldn’t reccomend blocking to large of a range. That way if there’s other quality reffers from that IP range, you won’t be blocking them. For example, you would want to be more specific than just blocking: 123.0.0.0

Hope this helps. Post any questions, and I’ll make sure they get an answer.