How to block access to your site by referrer
Do you have traffic coming from a source that you’d prefer not to be associated with? Is there a forum hotlinking to one of your images and using up your bandwidth? Fortunately there’s a pretty easy way to block these connections with a small text file called .htaccess.
Before we start, you need to be on a Linux server running Apache and not using the Front Page extensions for this to work. If you are using the Front Page extensions you can seriously screw up your site by editing .htaccess.
Also note that the filename is .htaccess with a period as the first character. That’s important. Otherwise the file won’t be found by Apache. It also has to be all lower case since Linux is a case sensitive file system.
And one last caveat, make sure you create or edit the .htaccess file in a text editor like Notepad. Do not use a word processor to edit the file since most word processors insert extra stuff in the files.
Below is a simple .htacess file that will block referrers from example.com.
-
RewriteEngine on
-
RewriteCond %{HTTP_REFERER} example\.com [NC]
-
RewriteRule .* - [F]
Line 1 turns the rewrite engine on. You may already have this line there if you’re using a CMS package, and if so don’t add it again.
Line 2 is the rule that matches the referral from example.com. Note the misspelling in HTTP_REFERER. It was misspelled in the original specifications so it’s misspelled here. Also note that it’s example\.com, not example.com. It’s a regular expression match and a period needs a backslash in front to match. The [NC] at the end means it’s a case insensitive match so it will match referrers from example.com, EXAMPLE.COM, or ExAmPlE.cOm.
Line 3 is what tells your server that any referrer that matches - the .* means any request - the rule on line 2 should be sent a forbidden response - the [F].
If you want to block more than one domain from referrals you can just stack them.
-
RewriteEngine on
-
RewriteCond %{HTTP_REFERER} example\.com [NC,OR]
-
RewriteCond %{HTTP_REFERER} badsite\.com [NC]
-
RewriteRule .* - [F]
Adding OR into the brackets after a rule means that if either rule matches then follow the rule below.
Question, Comments...
Do you have more questions. Please either leave a comment below or join us in our new forum.