Installation

Debian way

apt-get install libapache2-mod-geoip

Module source can be found on maxmind site:

http://www.maxmind.com/download/geoip/api/c/GeoIP-1.3.8.tar.gz
http://www.maxmind.com/download/geoip/api/mod_geoip2/mod_geoip2_1.1.5.tar.gz

Build

/usr/local/apache2/bin/apxs -i -a -L/usr/local/lib -I/usr/local/include -lGeoIP -c mod_geoip.c
ln -s /usr/local/apache2/conf/httpd.conf /etc/apache2/httpd.conf

Restart apache

/usr/local/apache2/bin/apachectl stop
/usr/local/apache2/bin/apachectl start

Redirection with mod_geoip and mod_rewrite

Below is an example of how to perform redirection based on country with mod_geoip and mod_rewrite. This configuration should be added to your Apache httpd.conf or .htaccess file.

GeoIPEnable On
GeoIPDBFile /path/to/GeoIP.dat

RewriteEngine on
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^CA$
RewriteRule ^(.*)$ http://www.canada.com [L]

The following Apache configuration directives block traffic from: China and Russia:

GeoIPEnable On
GeoIPDBFile /path/to/GeoIP.dat

SetEnvIf GEOIP_COUNTRY_CODE CN BlockCountry
SetEnvIf GEOIP_COUNTRY_CODE RU BlockCountry
# ... place more countries here

Deny from env=BlockCountry

GeoIPEnable On
GeoIPDBFile /etc/apache2/geoip/geoip.dat

Block a list of countries

RewriteEngine on
RewriteMap  countries-deny  txt:/etc/apache2/countries.deny
RewriteCond ${countries-deny:%{ENV:GEOIP_COUNTRY_CODE}|NOT-FOUND} !=NOT-FOUND
RewriteRule ^/.* http://www.examplearts.com [L]

File /etc/apache2/countries.deny

RU Russia
UA Ukraine
BY Belarus
YU Yugoslavia
BN Brunei
ID Indonesia
TJ Tajikistan
RO Romania
BG Bulgaria
IL Israel
PK Pakistan
MY Malaysia
KZ Kasakstan
IR Iran
EG Egypt
AF Afghanistan
IQ Iraq
DZ Algeria
MA Marocco
SA Saudi Arabia
KW Kuwait
GE Georgia
LY Libya
SY Syria
EA United Arab Emirates