Can I Block an Entire Region or Country from Seeing My Site? | HostGator Support
  1. Knowledge Base
  2. >
  3. Results
  4. >
  5. Can I Block an Entire Region or Country from Seeing My Site?

Can I Block an Entire Region or Country from Seeing My Site?

Methods for Denying Access to Specific Regions or Countries

There are three methods by which you can prevent visitors from a specific location from visiting your website, which varies in complexity.

Custom Rules in CloudFlare
It is important to note that for this method to work, a CloudFlare account is required, and it must be enabled for the domain you wish to block traffic for. To enable Cloudflare for your domain, you may follow the instructions available here before following these directions.

In addition to the other services offered by Cloudflare, you can use this service to easily block individual IPs, IP ranges, and countries from visiting your site. This is the simplest and most direct way to accomplish this.

  1. Log in to your Cloudflare account and click Dashboards at the top of the page.
  2. At the top right corner of your Dashboards page, click Threat Control.
  3. In the Add Custom Rule box, type an IP address, IP range, or country.
  4. Click Block +.

This will add a new rule to your Block List, which will deny visitors matching the rules you selected from reaching your domain.

This method will prevent your cPanel from being accessed via the domain by blocked locations (in addition to the website itself).

The Apache Deny Directive

The Apache Deny directive allows you to block visitors from accessing your Linux server from a given IP address. Using this method in .htaccess will provide you with direct control over access to your HostGator server and block unwanted visitors. Incorrectly used, however, it can instead have adverse effects on your traffic and site performance.

Unfortunately, there is no method for blocking traffic per IP address in Plesk. If you have Windows hosting, please contact us via phone or chat for assistance.
Blocking a Country or Region with .htaccess Deny Rules

Although it is possible to block an entire region or country using this method, we strongly recommend against it.

Using deny directives will cause Apache to check the entire list for matches on every single request, of which there may be dozens per single page load. This directly affects the performance of your website and the server as a whole.

Therefore, we discourage the use of this technique unless there are compelling reasons to do so. If used, we ask that you strictly limit the number of IPs blocked.

If you would like our assistance with blocking a large number of IPs, please contact us via phone or chat for assistance so that a custom Apache configuration can be made.

 

Using a Combination of CloudFlare and .htaccess Rules
It is important to note that for this method to work, a CloudFlare account is required, and it must be enabled for the domain you wish to block traffic for. To enable Cloudflare for your domain, you may follow the instructions available here before following these directions.

This option offers the most flexibility with the most convenience. Cloudflare has a feature enabled by default that includes IP GeoLocation Headers in the requests for your site. These headers can be used to block regional access to different areas of your website by using .htaccess Deny rules. Here, for example, is .htaccess code for blocking traffic from India and the United States when visited through a domain using Cloudflare:

SetEnvIf CF-IPCountry IN BuzzOff=1
SetEnvIf CF-IPCountry US BuzzOff=1
Order allow, deny
Allow from all
Deny from env=BuzzOff

By editing this code to add a line for any country you wish to block and using the correct country code, you can effectively restrict access to different parts of your site or the site as a whole. A complete list of country codes may be found here. For instance, by changing SetEnvIf CF-IPCountry IN BuzzOff=1 to SetEnvIf CF-IPCountry CN BuzzOff=1, you would block China instead of India, or by adding a third line, SetEnvIf CF-IPCountry CA BuzzOff=1, you could block Canada as well.

Blocking Specific IPs

If you already know a specific IP address you want to block, please see the following article for detailed instructions: