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

Is there a way to use .htaccess to block access to a country?

I have been asked to keep our server from allowing requests to download a certain file from being downloaded from the US. Downloading from any other country is fine.

Is there a way to do this in an .htaccess file?

share|improve this question

migrated from Aug 5 '09 at 16:09

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

Eh, I don't know... this sort of setup is useful for, say, material that's copyrighted in the U.S. but not in other countries. Anyway, belongs on ServerFault. – David Z Aug 5 '09 at 15:54
No it is for a financial report which is not to be accessed from the American stock market before after closure today. – Eskil Mjelva Saatvedt Aug 5 '09 at 16:00
Oh, that's sketchy. If this is [time-]sensitive information that might have legal ramifications if accessed, I might not even host it publicly if I could help it. But IANAL. – Rob Hruska Aug 5 '09 at 16:03

If you are using Apache, take a look at mod_geoip. Taking from their example, you would configure it with something like:

GeoIPEnable On
GeoIPDBFile /path/to/GeoIP.dat

# ... place more countries here

Deny from env=BlockCountry
share|improve this answer

I'm pretty sure there's no way you can do this with full effectiveness. Even if you do find a way to restrict based on IP or provider, I can still go through a proxy server in an "allowed" location to get the file.

share|improve this answer

There may be more reliable ways, but you can get close by doing an IP lookup of the requestor and denying those that are US IPs.

Note that this is far from full proof, and is easily circumvented if the requester is motivated to do so.

share|improve this answer

You can with varying degrees of reliability detect the country form the IP address and then use .htaccess in apache to block it.

See the examples at Htaccess country block API

share|improve this answer

You will want to use a Geo Intelligence Data provider. The data for the mod_geoip Apache module (I believe) is provided by MaxMind, but their are more accurate/reliable solutions depending on your budget and needs.

Data Providers include:
Digital Envoy - Considered the industry leader



All providers have multiple levels of data available including IP domain, connection speed, time zone, etc. if you ever needed to filter or change content on more granular data. Depending on the provider, there are multiple ways to access the data whether it be a flat file you load into your own db, in-house servers through an API, or web-services.

You will want to be sure to get something that is updated on a regular (i.e. weekly) basis as IP's change location quite frequently.

share|improve this answer

This is an old question but in case it crops up again - You need to consider that it's possible to use a proxy in another part of the world to gain access to sites and files that you may not have access to directly. It follows therefore that such a restriction is pretty much pointless and there may well be legal issues to consider.

share|improve this answer

Your Answer


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