How to Block Fake Referral Traffic via htaccess

I recently talked about how to remove spammy referral traffic in Google Analytics by creating URL specific filters to remove the offending referral sources from appearing in your traffic. But what if we wanted to cut off the problem at the source and keep these sites from being able to send any traffic to our sites? In this article we talk about how to block fake referral traffic via htaccess.How to Block Fake Referral Traffic

The htaccess file for our website is a very powerful tool which can be used a number of different ways. It can be used to communicate certain things to search engines and our user’s browsers about our website, but more importantly here it can be used as the gatekeeper of our website.

If your website doesn’t already have an htaccess file, it’s simple to make one from nothing. You simply need access to the file manager of your hosting account to do this, so let’s walk through the simple process of how to block fake referral traffic via htaccess.

How to Block Fake Referral Traffic

If you already have an htaccess file on the root directory of your website, open it up and drop down to the bottom of the file. If you don’t happen to already have one, simply create a new file and name it .htaccess. It handles like a text file but search engines and browsers recognize the extension.

Once you’re in your .htaccess file, paste this code in, starting on its own line:

RewriteEngine on
RewriteCond %{HTTP_REFERER} example\.com [NC,OR]
RewriteCond %{HTTP_REFERER} example2\.com [NC,OR]
RewriteCond %{HTTP_REFERER} example3\.com [NC,OR]
RewriteCond %{HTTP_REFERER} example4\.com [NC]
RewriteRule .* - [F]

Replace the example/.com with the URL you want to block, noting to change the domain’s extension as necessary. The NC stands for no case, or not case sensitive, and the OR simply means follow on to the next line.

Now, when an offensive domain tries linking to your site, any traffic sent to your site from said offending site will instead be met with a 500 internal server error.

Why do it this way?

Technically there’s no harm in letting spammy sites link to your site. This comes from a practicality point of view.

Many webmasters don’t use Google Analytics or don’t have any idea of which sites are linking to or sending traffic to their sites. It wouldn’t be fair to punish them for something they had no awareness of.

But I say if we can prevent it when we know it’s happening, why not go ahead and do it?

Furthermore, this is a much more efficient way to block fake referral traffic from showing up in your analytics reporting, or cutting it off at the source.

Creating filters in Google Analytics takes a lot longer than simply copying and pasting in any and all offending linking to sites using the htaccess rewrite code listed above.


Scroll to Top