Ove
Ove

Reputation: 21

How Can I Block Visitors By Country?(Htaccess)

Lest say that i want to block visitors from spesific country, what is the right method to write it?
i tried to use deny from ip
but this is not the right way to do it.

Upvotes: 1

Views: 7615

Answers (5)

Jamie
Jamie

Reputation: 808

I would recommend using the PHP Extension API if you are concerned at all about performance. You can get upwards of 7 million queries per second with the PHP (C API) extension. htaccess is known to have performance side-effects.

I describe how to compile the extension, and how to use the mmdb databases in PHP to restrict countries by iso-3316 2-character codes here:

Intro to Maxmind GeoLite2 with Kohana PHP

Geolocate IP Address

Upvotes: 0

Tomasz Kajtoch
Tomasz Kajtoch

Reputation: 11

If it must be .htaccess file then you can use auto generator tool avaiable on ipinfodb.com. You can also use API of geolocation services, like for example: http://freegeoip.net/static/index.html.

Upvotes: 1

Josua M C
Josua M C

Reputation: 3158

Blocking specific networks using Apache's .htaccess file is pretty easy. For instance, adding...

deny from 221.140.10.0/24

..would block any traffic from the 221.140.10.0 Class-C network (AKA a slash-24).

So blocking specific countries is just a matter finding out all the networks in those countries, then creating a series of entries like the above.

this reference link might be help you

Upvotes: 0

VettelS
VettelS

Reputation: 1224

Any method you use is never going to be full-proof. Users can use proxies to make them appear to be in a different geographical location to where they actually are. If it is critical that you block users from accessing your site from certain areas, you would have to think about different methods (that don't rely on IP addresses) of authentication.

Upvotes: 0

Oussama Jilal
Oussama Jilal

Reputation: 7739

The right way is to not use an htaccess, you can do some PHP cheking using an already filled table with ip intervals that tells you wich interval is affected to wich country.

Edit:

If you want to use htacces, you will have to write a deny from ip line for each ip range in a country (if you try to block 10 countries for example, this may result in a mess and this is why it is a bad idea).

Upvotes: 1

Related Questions