How To Block Referrer Spam

 ·  · 

Notes about how to block referrer spam.

 

1. Block Referrer Spam In Google Analytics

Login your Google Analytics account and switch to Admin tab, then click All Filters entry in the left navigation bar.

how-to-block-referrer-spam-in-google-analytics

Click New Filter button to add a new filter. Input a filter name, for example Referer_Spams.

how-to-block-referrer-spam-in-google-analytics

Select the Custom filter type, and choose the Exclude filter. Select Campaign Source in Filter Field dropdown list.

how-to-block-referrer-spam-in-google-analytics

Input the domains you want to block in the Filter Pattern field, using | as separator.

success-seo.com|videos-for-your-business.com|buttons-for-website.com

Save the filter.

 

2. Block Referrer Spam Using Nginx

Create a blacklist map file:

$ sudo vim /etc/nginx/conf.d/referer_blacklist.conf

Add map entries to referer_blacklist.conf:

##
# Referer spam map list
##
map $http_referer $spam_referer {
    hostnames;
    default                                 0;
    "~*videos-for-your-business.com"        1;
    "~*success-seo.com"                     1;
    "~*buttons-for-website.com"             1;
}

Note: The ~* prefix means case-insensitive matching. If matching is successful the 1 will be assigned to $spam_referer variable so that it will be evaluated to true.

Then make sure include conf files in http block of your /etc/nginx/nginx.conf.

# /etc/nginx/nginx.conf

http {
# ...
  include /etc/nginx/conf.d/*.conf;
# ...
}

Lastly add checking and blocking in Nginx configuration file of your site:

# /etc/nginx/sites-available/your_site.conf

server {
    ...

    if ($spam_referer) { 
        return 444; 
    }
}

Test the configuration and restart/reload Nginx:

$ sudo nginx -t
$ sudo service nginx restart

# or
# $ sudo service nginx reload

 

3. Block Referrer Spam Using Apache

If the blacklist is short, you can directly block the spam domain using .htaccess:

RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_REFERER} spam1.com
RewriteRule ^.* - [F]
RewriteCond %{HTTP_REFERER} spam2.com
RewriteRule ^.* - [F]
RewriteCond %{HTTP_REFERER} spam3.com
RewriteRule ^.* - [F]

If your blacklist is long you can maintain it separately.

Create a blacklist for spam domains:

$ sudo touch /etc/apache2/spam_blacklist.txt

Add spam entries into the blacklist file.

spam01      -
spam02      -
spam03      -

Load and map this blacklist in /etc/apache2/apache.conf:

Rewritemap refhashmap txt:/etc/apache2/blacklist.txt

Then add the blacklist checking in your virtual host configuration file.

RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_REFERER} ^http://([^/]+)
RewriteCond ${refhashmap:%1} ^-$
RewriteRule ^.* - [F]