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 vary in complexity.
Custom Rules in CloudFlare
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.
- Log into your CloudFlare account and click Dashboards at the top of the page.
- At the top right corner of your Dashboards page click Threat Control.
- In the Add Custom Rule box type an IP address, IP range, or country.
- 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.
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 give you direct control over access to your HostGator server and can block unwanted visitors. Incorrectly used, however, it can instead have negative effects on your traffic and site performance.
Blocking a Country or Region with .htaccess Deny Rules
Although it is possible to block an entire region or country using this method, we very 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 performance of your website and the server as a whole.
Therefore, we discourage 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.
Using a Combination of CloudFlare and .htaccess Rules
This option offers the most flexibility with the most convenience. CloudFlare has a feature enabled by default in which 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
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: