Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a website that I do not want to be viewed by entire countries. The website is running on a LAMP server. How can I achieve this?

share|improve this question

migrated from Aug 18 '09 at 10:50

This question came from our site for professional and enthusiast programmers.

There is nothing more anoying than access denytion becaus you live in a specific country. – rightfold Aug 18 '09 at 8:01
I also do not recommend it, but it is a requirement of the client – haim evgi Aug 18 '09 at 8:04
Except for getting sued because of sharing content with people not eligible for it :). – Zed Aug 18 '09 at 8:33
It is impossible to block access from countries, the internet is not configured that way. You will get many false positives and false negatives. – Jacco Aug 18 '09 at 8:42
there are many legal reasons to block countries, for example some countries do not allow online gambling and if you happen to run a gambling site and don't want to get into jail in that country, you might need to block those countries. – rytis Feb 1 '10 at 20:03
up vote 10 down vote accepted

Use mod_geoip module.

For example:

GeoIPEnable On
GeoIPDBFile /path/to/GeoIP.dat


# ... place more countries here
Deny from env=BlockCountry
share|improve this answer

You can use rewrite_mod's REMOTE_ADDR condition to redirect banned users to a single page describing the fact that they are not allowed to enter, or simply give them a 403 error.

RewriteCond %{REMOTE_ADDR} ^123\.123\.123\.[0-9]{3}$ 
RewriteRule .* ...

To get the range of IPs for a country, get the Maxmind database for example.

share|improve this answer
+1 for referencing a source, though I don't think using rewrite rules is the way to go, the list might be quite large! – Paul Dixon Aug 18 '09 at 7:58
You are absolutely correct. It all depends on how many and what type of countries to block... – Zed Aug 18 '09 at 8:31

Check the IP and determine which country hosts it, and then block it. Of course it's not accurate, but it's something. Generally I'm not a fan of doing this though; but maybe it's required for some legal reason.

share|improve this answer

You can filter the country ISP's ip adresses. There are a lot of geolocation database, to help you to identify the users country by ip.

share|improve this answer

I have came across this site, which claims to ban country,

IMO, the best way it to block using the .htaccess file

share|improve this answer

You can use services like this:

to extract info which IP ranges belong to which countries and block them. Keep in mind if you block too many ranges on a high traffic web site you may see some heavy utilisation on your firewalls.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.