Derek Kwok's Blog

Blog about Software Development, Techniques and Discoveries.

Ubuntu+Apache and GeoIP Tutorial

| 2 Comments

Motivation

You have a site (say http://www.example.com), with different subdomain or urls mapped for users from different countries. In this example, we’ll assume something along these lines:

  • http://ca.example.com (for users from Canada)
  • http://jp.example.com (for users from Japan)

If you are using Apache httpd to serve your website, you can use mod_geoip to make the redirect happen.

In this tutorial, you are assumed to be using Ubuntu with apache to serve your website.

Installing mod_geoip for Apache in Ubuntu

sudo apt-get install libapache2-mod-geoip

This will automatically install Maxmind’s geoip module and enable it.

Using mod_geoip in httpd.conf or .htaccess

Below is a snippet of code showing an example configuration using geoip. In the example, the user will be redirect to http://ca.example.com if they are from Canada and http://jp.example.com if they are from Japan. The redirect syntax is regular apache mod_rewrite syntax (i.e. you can use regular expression and group matching to better redirect).

    GeoIPEnable On
    GeoIPDBFile /usr/share/GeoIP/GeoIP.dat

    RewriteEngine on
    RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^CA$
    RewriteRule ^(.*)$ http://ca.example.com

    RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^JP$
    RewriteRule ^(.*)$ http://jp.example.com

You can find the list of countries available at: http://www.maxmind.com/app/iso3166

Sources for this tutorial:

2 Comments

  1. hi there im working on something like this and i cant get it work properly.

    1. main server is located in london, using geoip to get continet code to show of a visitor connecting to web server. there are several web domains connecting to it as http and www as well

    2. i want to incoming header and address using continent geoip code forward it to another reverse proxy located in another continent using geoip. reverse proxy has already a database of running web addresses and where to looking for files on original web server

    3. i just do not know how to use header coming to main server and forward it to reverse proxy so reverse proxy would see it as coming straight to it. main server should not have a database of running web sites. just a plain redirection using geoip to reverse proxy on another continet

    i know this shouldbe easy i just need help

    thanx, john

  2. Good day. The command line at the top where do I go to run this? I am running a windows machine with xampp. I am a newby to this and have no clue what you are trying to show in the article above. Please can you help me out.

Leave a Reply

Required fields are marked *.

*