Lately I have been getting some referrer spam in my website's logs. In order to stop this (as it's quite annoying and also polluting my statistics), I want to block requests that are coming from certain referrer domains. A lot of articles can be found that recommend putting the following (or something similar) in the website's .htaccess file:
RewriteEngine On
RewriteCond %{HTTP_REFERER} example\.com [NC]
RewriteRule .* - [F]
This works fine, however, I would like to move this to the Apache configuration, in order to apply this rewrite rule to all websites hosted on the server. I've also read that this is better for perfomance because the .htaccess file has to be parsed for every request, whereas the Apache configuration is only loaded once.
My server is running on Ubuntu, so I created a new .conf file: /etc/apache2/conf-available/block-referrer-spam.conf, with the exact same content as the .htaccess snippet from above. I created a symlink in the conf-enabled directory (using sudo a2enmod block-referer-spam
) and reloaded the Apache configuration (sudo service apache2 reload
). However, unlike when using .htaccess, Apache does not block the request (return a 403).
I think I might be overlooking something simple, so any help is appreciated.