1

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?

3
  • 1
    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, 2009 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, 2009 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, 2009 at 16:03

6 Answers 6

10

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

SetEnvIf GEOIP_COUNTRY_CODE US BlockCountry
# ... place more countries here

Deny from env=BlockCountry
7

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.

0

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.

0

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

0

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

MaxMind

Quova

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.

0

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.

You must log in to answer this question.