Skip to content

Instantly share code, notes, and snippets.

@s-thom
Last active August 29, 2015 14:19
Show Gist options
  • Save s-thom/a705c28e0f254b400218 to your computer and use it in GitHub Desktop.
Save s-thom/a705c28e0f254b400218 to your computer and use it in GitHub Desktop.
Get rid of referral spam in analytics

#Add this to your .htaccess file

# Referral spam blocker
<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteCond %{HTTP_REFERER} (INSERT_REFERER_HERE) [NC]
  RewriteRule .* – [F]
</IfModule>

##Explanation

<IfModule mod_rewrite.c>
  RewriteEngine On
  ...
</IfModule>

Make sure that rewriting is available, and turn it on.

RewriteCond %{HTTP_REFERER} (INSERT_WEBSITE_HERE) [NC]

Check the HTTP Referer, and if it contains the text "INSERT_WEBSITE_HERE", then do the action.
If you want to check for multiple referers, just repeat this line for each referer, and add the OR flag ([NC,OR]).
The flag NC means it will ignore cases. i.e. EXAMPLE.COM is equivalent to example.com.

RewriteRule .* – [F]

Here we just throw everything that matched away with a 'Forbidden' status code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment