Only Allow Access from Specific Countries to Your Proxy Site

Posted by Vectro 8 August 2011

Some proxy websites are only targeted to one or more specific countries and might need to block all others. There are two ways to accomplish this and both require the GeoIP Apache module to be installed on your server. Contact your hosting provider to find out if it is available or to request to have it installed. The code examples below should be placed in your .htaccess file.

Blocking

Block all of the countries except the allowed ones, returning a 403 ‘Forbidden’ error to the user.

The following code will block a single country:

RewriteEngine On
SetEnvIf GEOIP_COUNTRY_CODE XX AllowCountry
Deny from all
Allow from env=AllowCountry

Replace XX with the 2-character code of the country you want to block. You can find the ISO-3166 country code list here.

The example below would only allow America, Russia and China:

RewriteEngine On
SetEnvIf GEOIP_COUNTRY_CODE US AllowCountry
SetEnvIf GEOIP_COUNTRY_CODE RU AllowCountry
SetEnvIf GEOIP_COUNTRY_CODE CN AllowCountry
Deny from all
Allow from env=AllowCountry

Redirecting

Redirect traffic from all countries except the allowed ones. In this case, you can specify a URL and users from non-allowed countries will be redirected to it.

The following example would allow access from America, Canada and France while redirecting users for other countries to www.example.com:

RewriteEngine on
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} !^(US|CA|FR)$
RewriteRule ^(.*)$ http://www.example.com$1 [L]

It is possible to do this for as few or as many countries as you want. Each country code should be separated by a | symbol. If there is only one country, do not use the | symbol. The ISO-3166 list of country codes is here.

Sorry, comments are closed.

Previous Post
«
Next Post
»