[How-To] Block Visitors From Certain Countries To Your Website

If you are someone who is in the process for setting up a website or an offering specifically for users from a certain country, you need to deny visitors from all other countries ( except the country which you are targetting). The denial process is pretty simple and straight ( involving modifications to your .htaccess file) and this article describes how you can setup your website to deny any traffic from specific countries.

In several web servers (most commonly Apache), .htaccess (hypertext access) is the default name of a directory-level configuration file that allows for decentralized management of web server configuration. The .htaccess file is placed inside the web tree, and is able to override a subset of the server’s global configuration; the extent of this subset is defined by the web server administrator.[1] The original purpose of .htaccess was to allow per-directory access control (e.g. requiring a password to access the content), hence the name. Nowadays .htaccess can override many other configuration settings, mostly related to content control, e.g. content type and character set, CGI handlers, etc.

For the sake of simplicity, lets assume, you wish to deny all the traffic to your website originating from Afghanistan. For you to be able to deny the traffic at the webserver level, you need to be aware of two key facts.

1.) Every country has a public IP Address block

Yes, every country has a specific IP address block, what this means is, that any visitor coming from a country ( say India) can only have an IP address which is part of the IP address block for that country. Though, if the end user is making use of a VPN or a proxy gateway which is located in some other country, the IP Address presented to the webserver can be very different.In my previous post titled List of Major IP Addresses Blocks By Country, i wrote about how you can find IP address blocks for countries.

2.) Modify your .htaccess file to deny traffic from specific IP Address blocks

Once you have identified the IP Address block for the country as mentioned in step 1, you need to modify your .htaccess file. As mentioned above, .htaccess file can be specified to have access control features.For example, you must add the following snippet to your .htaccess file to block traffic from Afghanistan

ErrorDocument 403 http://www.technofriends.in
<Limit GET HEAD POST>
order allow,deny
deny from 58.147.128.0/19
deny from 117.55.192.0/20
deny from 121.100.48.0/21
deny from 125.213.192.0/19
deny from 202.56.176.0/20
deny from 202.86.16.0/20
deny from 203.215.32.0/20
deny from 210.80.0.0/19
deny from 210.80.32.0/19
allow from all
</LIMIT>

If you are not comfortable with writing such snippet, you can also visit a free website at www.blockacountry.com which allows you to select the countries you want to block from your website. Pressing “Go” button generates the required snippet. Copy and paste the generated code into your .htaccess document and visitors from the selected country will not be able to access your website.

If you are someone who wants to block the traffic right at the Firewall level and not let the traffic enter the webserver at all, you can do so by modifying your Firewall configĀ  to block IP address blocks of the specific country ( as obtained in step 1).

Also Read: [How-To] Block Specific Domains Using OpenDNS

Block Websites of your choice without any Software

You can follow me on Twitter at http://twitter.com/vaibhav1981

Do stay tuned to Technofriends for more, one of the best ways of doing so is by subscribing to our feeds. You can subscribe to Technofriends feed by clicking here.

  • Share/Bookmark

Related posts:

  1. [How-To] Disable Comment Posting for No Referrer Requests
  2. [How-To] Block Specific Domains Using OpenDNS
  3. [How-to] Block access to Control Panel in Windows XP
  4. Vonage Is Offering Unlimited Calls To More Than 60 Countries For USD $25 Per Month
  5. List of Major IP Addresses Blocks By Country

Leave a Reply