GeoIP block country

The free edition of the GeoIP database is not as accurate as the subscription based database.

Test this by working inside a non important testing directory with a simple testing file such as:

www.site.com/mytesting/index.html

Create a new .htaccess file with the following contents:


SetEnvIf GEOIP_COUNTRY_CODE GB BlockCountry
Deny from env=BlockCountry

This will block GB from your testing directory. Note, this document is only intended to illustrate the possibilities of the use of GeoIP for a basic how to.

Or If you want to redirect based on the country using mod_rewrite in combination with mod_geoip, your .htaccess file could look like this:


RewriteEngine on
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^(NL|BE)$
RewriteRule ^(.*)$ https://www.mydomain.com/nl/$1 [L]

You do not need to enter the path in your .thaccess file:
GeoIPDBFile /path/etc/
Doing so will result in a 500 server error!

Please see the following 3rd party site, the makers of GeoIP for further details:
https://dev.maxmind.com/geoip/legacy/mod_geoip2/