Using GeoIP with Magento and Multiple Sites

The GeoIP database and API from MaxMind provide an easy way to determine country and region from an IP address. This can be used on a website to determine which country a site visitor is from, to provide them with a tailored experience, or to direct them to an alternative website.

Combined with Magento's multi-store capabilities, this can be used to provide the most suitable currency, language and product selection automatically.

One thing I wanted to be careful with, was to make sure that my implementation didn't have a negative effect on SEO. For example: redirecting to a country specific domain based on IP address might stop some domains from being indexed by search engines (Googlebot IP addresses are nearly always US based). Also, having the same domain to serve different countries might mean lower (or no) search ranking for country specific searches.

There are lots of different scenarios that are possible, and I can't cover them all, but hopefully you'll find something useful below. Let's get started...

Installing GeoIP

GeoLite Country is the free GeoIP database (there is a commercial one with more accuracy and frequent updates). There are also various provided APIs (open-source) to access the database.

Pure PHP API

The PHP API is easy to set up and use but has the disadvantage of being a lot slower than the C API/mod_geoip (see benchmarks). However if you are on a shared host, or for some reason don't have root access on your server, this may be your only solution.

Just download the geoip.inc file and the GeoIP.dat.gz database and save them into a geoip sub-folder in your document root. (uncompress the GeoIP.dat.gz to GeoIP.dat)

To make use in Magento, you could include something similar to the following in your index.php

Refer to the Magento Wiki for further information about multiple websites with Magento.

Since I'm using the C API with mod_geoip, I won't give much more attention to the PHP API.

more to come...