apache-mod_geoip

This is an old post from my previous blog. I'm not sure if the information here is relevant for the latest versions of Apache, GeoIP, etc. Here is the original post:

I had to implement http redirection to pages in different languages, based on visitors' geographic location. E.g. if you are in Germany, Bulgaria or France - you are redirected to a page in German, Bulgarian or French. Everyone else is redirected to the English content. To accomplish this I used mod_rewrite and GeoIP. GeoIP is a product of MaxMind. They provide free database, but the updates for it are payed. I don't remember the exact price, but it was really affordable. MaxMind has Apache module - mod_geoip. It is installed with GeoIP's C API - both are available at their website. API compilation process is straightforward. In order to compile the Apache module you should do this:

apxs -i -a -L/usr/local/lib -I/usr/local/include -lGeoIP -c mod_geoip.c

Where:

-I/usr/local/include - the directory where GeoIP.h is located

-L/usr/local/lib - the directory where libGeoIP is installed

Next you should configure Apache itself. Here is a part from my configuration file:

GeoIPEnable On #Activates mod_geoip
GeoIPDBFile /usr/local/share/GeoIP/GeoIP.dat #Path to the database
RewriteEngine On #Enable mod_rewrite
#By default - redirect to the English version
Redirect /index.html http://www.example.com/index?lang=en
#The visitors from Bulgaria are redirected to Bulgarian content
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^BG$
RewriteRule ^/?(index.html)?$ http://www.example.com/index?lang=bg [L]
#Visitors from Germany - to German content
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^DE$
RewriteRule ^/?(index.html)?$ http://www.example.com/index?lang=de [L]
#France - french content
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^FR$
RewriteRule ^/?(index.html)?$ http://www.example.com/index?lang=fr [L]

%{ENV:GEOIP_COUNTRY_CODE} is a variable, which contains country's two letter according to ISO 3166-1.

More information about mod_rewrite and regular expressions can be found here:

  • mod_rewrite documentation on Apache web site

  • The book Mastering Regular Expressions of Jeffrey Friedl

  • The book The Definitive Guide to Apache mod_rewrite of Rich Bowen

Comments

Comments powered by Disqus