How to block traffic to your website from a particular domain?

If you are getting a lot of traffic from a particular domain that it causes load on your hosting server and subsequently suspending of your hosting account by your host, than you can use this .htaccess code to block that particular domain. This trick need your server to be based on Apache or Litespeed. Here are the steps to block a particular domain:

  1. Create a .htaccess file in the root folder of your website. If you want to block traffic to some particular directory or folder than create .htaccess file in that particular folder.
  2. After creating .htaccess file paste the following code in the file after modifying it to your own requirements.

RewriteEngine ON

RewriteCond %{HTTP_REFERER} example11\.extension[NC,OR]

RewriteCond %{HTTP_REFERER} example22\.extension[NC,OR]

RewriteCond %{HTTP_REFERER} example33\.extension

RewriteRule .* – [F]

There might occur a situation in which you start getting Errors on visiting your website than add the following line after RewriteEngine ON

Options +FollowSymlinks

In case you want to block traffic from a single domain than add the following code to your .htaccess. Here we just removed the OR directive:

RewriteEngine ON

RewriteCond %{HTTP_REFERER} example1\.extension[NC]

 

RewriteRule .* – [F]

Code Explanation:

  • example11 or example22 or example33 are the domain name which you want to block.
  • extension basically refers to the ending part of the domain name that is .com, .net, .org etc.
  • NC directive is for making the domain name case insensitive which means example11.com is same as EXAMPLE11.COM
  • OR directive is used for adding multiple domains.
  • F directive is used for producing Forbidden Error ( 403 )