Can I block a referring site from my web site

Avatar
  • Answered
While reviewing visitors to my site (within Cpanel) I noticed referrals from a porn site :-(
Is there a way to block such visitors or referring sites?
Avatar
JacobIMH
Hello waltmi, and thanks for the great question. It can be a pain when you get traffic from sources you'd rather not. Luckily you do have an .htaccess file that can control access to your site. I've written a guide already on how to block unwanted users from your website that covers getting to your .htaccess file, and also blocking by IP address or User-Agents. Thanks to your question I'll go ahead and update that guide with bad referrals as well. In the meantime, here's how you would block referral traffic from just example.com:
RewriteEngine On
RewriteCond %{HTTP_REFERER} example\.com [NC]
RewriteRule .* - [F]
If you wanted to block referral traffic from both example.com and example.net:
RewriteEngine On
RewriteCond %{HTTP_REFERER} example\.com [NC,OR]
RewriteCond %{HTTP_REFERER} example\.net
RewriteRule .* - [F]
Any user coming from a site that matches one of those referral URLs will be served a 403 forbidden error page. You can follow the same steps for how to setup a 404 error page in .htaccess as well as create a custom 404 error page to customize a 403 error as well. Thanks again for your question, and please let us know if you had any more! - Jacob