Store/Website Redirect Based On GeoIP in Magento

Mini Tutorial: Store/Website Redirect Based On GeoIP in Magento

March 26, 2013 by Allen

Have you ever wanted to redirect your customers to the correct store site based on their GeoIP? In this tutorial, I’ll show you the basic requirement of setting up a store redirection in Canada and the US.

First step is to install the following extension, GeoIP by OpenStream. It syncs the GeoIP database that is freely provided MaxMind. Click here for the accuracy of this GeoIP database

Second step is setting up a script at web root directory. I called it, geoipredirect.php

<?php
Mage::app();
$geoIP = Mage::getSingleton('geoip/country');
$country = $geoIP->getCountry();

if(strcmp($country,'US') == 0) {
    $mageRunType = 'website';
    $mageRunCode = 'US';
}
else
{
    $mageRunType = 'website';
    $mageRunCode = 'CA';
}
Mage::reset();

Third step is to include above script in index.php. Include the script right before the run function!

.....

require_once("geoipredirect.php");

Mage::run($mageRunCode, $mageRunType);

The last step is to wait for sales!!!! =)

Update: Fatal error: Class ‘Mage’ not found in geoipredirect.php on line 2
You can try copying the GeoIP Redirect code directly to the index.php

<?php
... Default Magento Code ...
$mageRunCode = ....
$mageRunType = ....
require_once("geoipredirect.php");
<Insert GeoIP Redirect Code (Second Step) here>
Mage::run($mageRunCode, $mageRunType);
extension in my libray: Sandfox_GeoIP-master

Leave a Comment