How to GEO Redirect Based on Country

If you do any CPA marketing you’ll notice that most of the offers are restricted to certain countries or have different links depending on the country. Here’s what I do to redirect visitors to different links depending on the country as determined by the visitor’s IP address.

First you will need to install the PHP Net_GeoIP package onto your host. Most hosting providers already have this installed including the one’s I use Beyond Hosting and HostGator.

Next you will need to grab the latest GeoLite Country database binary from MaxMind here. Extract the archive and place the ‘GeoIP.dat’ file in the same directory as the script.

If you use Beyond Hosting, place it here:
/home/your-domain/php/Net/GeoIP.dat

Now comes the actual redirect script itself which we’ll call ‘redirect.php’.

<?php
// Next two lines are for Beyond Hosting
// Don't forget to change your-domain
require_once '/home/your-domain/php/Net/GeoIP.php';
$geoip = Net_GeoIP::getInstance('/home/your-domain/php/Net/GeoIP.dat');

// Next two lines are for HostGator
require_once 'Net/GeoIP.php';
$geoip = Net_GeoIP::getInstance('GeoIP.dat');

try {
  $country = $geoip->lookupCountryCode($_SERVER['REMOTE_ADDR']);

  switch((string)$country) {
    case 'AU':
      $url = "http://www.howwhowhen.com";
      break;
    case 'CA':
      $url = "http://www.howwhowhen.com";
      break;
    case 'GB':
      $url = "http://www.howwhowhen.com";
      break;
    case 'US':
      $url = "http://www.howwhowhen.com";
      break;
    default:
      $url = "http://www.howwhowhen.com";
  }

  header('Location: '.$url);
} catch (Exception $e) {
  // Handle exception
}
?>

 

Make sure you only include the section for Beyond Hosting or Hostgator only. Then change the URLs to your offer links. Now when a visitor goes to www.your-domain.com/redirect.php they will be redirected to the proper links.

If you have any questions just leave a comment below.

Comments

  1. Wonderful work! This is the kind of info that should be shared around the web. Shame on Google for not positioning this publish upper! Come on over and talk over with my website . Thanks =)

  2. Bo Boswell says:

    This is great! Thanks for publishing. One question, I have a working php file but I am wanting to integrate this with WordPress. Any idea where I put this PHP script in order to redirect users as soon as they hit my domain?

  3. Howie Nguyen says:

    Hi Bo, if you want it to hit your script right when they get to your domain then just name the script index.php and place it at the root of your domain.

    As far as integrating with WordPress can you give me an example of what you’re trying to do? I’ll see if I can help. Otherwise you might want to take a look at this:

    CPA Geo Plus

    It’s a WP plugin and might be what you’re looking for. I don’t use it myself as I don’t have any need for it though.

  4. Thanks for the script and easy setup. I’m stuck on a few things if you could assist?
    I have BeyondHostingt and it says to place the GeoIP.dat file in /home/your-domain/php/Net/GeoIP.dat. Are you referring to creating new folders within my domain for this? Or is this done in the Cpanel file manager?

  5. Howie Nguyen says:

    Mike, you shouldn’t have to create any folders. You can use cPanel to navigate to /home/your-domain/php/Net/ and then upload the file GeoIP.dat there.

    • Thanks for your fast reply Howie! I owe you for the support and I found this page from the EWA group. I opened Cpanel and found /home/my-domain/ but unfortunately there was no php folder after that. Sounds like I might need to contact Tyler at beyond unless you know of another solution.

      • Howie Nguyen says:

        In cPanel under Software/Services, open PHP Pear Packages. Then install Net_GeoIP. That should create the directories and install the needed packages for you.

        Tyler from BeyondHosting is awesome along with the rest of their support team. They might tell you that you don’t need to install anything or do any of this but what I found out is the stuff on their VPS (at least the Hybrid) does not contain the latest database. Doing it this way you can always have the latest database with accurate information.

        • Howie you are the man! I truly appreciate your help. Turns out I just needed to install the PHP Pear Packages which I’ve never done previously to this. I tested the script with about 5 countries using VPN and it works like a charm.

          Thank you so much!

  6. I am very happy to know that you are providing the GEO lite country databage binary from maxmind. It is great news for me. I want to get an ‘GeoIP.dat’. I am excited for this IP. Thanks for your nice post

  7. Hi, I’m trying to integrate this script with opencart, and I’ve placed the code require_once(‘redirect.php’); in the top section of my index.php file. However this causes a redirect loop – could you shed any light on this? I’m quite new to PHP so I’m not sure where to place the script. Thanks!

    • Howie Nguyen says:

      Where is your redirect.php script redirecting to? If it is redirecting to index.php then yes you will have a redirect loop.

  8. Hi Howie, this looks like a really cool script, so thank you! One question though, we plan to use this on a Joomla-based website; any tips on how best to integrate it? Or it should be pretty similar to WordPress?

    • Howie Nguyen says:

      It shouldn’t be any different whether it’s WordPress or Joomla. What exactly are you trying to do though. I can better help you if I know the exact scenario you’re trying to accomplish.

  9. I have encountered some problems installing this geo script. I am using Beyond Hosting and how do I install the files into the PHP pear packages? I can’t find the upload button

    • Howie Nguyen says:

      If you’re talking about the Net_GeoIP pear package you don’t need to install that on Beyond Hosting. It should already be done. If in doubt, you can try opening a support ticket with them to ask them to do it. They are super fast!

      If you want to upload the GeoIP.dat or redirect.php files then you can just use a regular FTP client such as FileZilla to do so. Hope that helps!

  10. Great article. Do happen to know perhaps how this can be written to allow/certain ips based on country using net_geoip?

  11. Hi guys.

    If anyone is looking for a hosted solution that automatically redirects users based on their location, we have made an easy-to-use solution: http://www.splitterhq.com/insights/redirect-based-on-country

    It’s on a country level for now, but we are working on region and city based redirects. We do device type and OS detection and more as well.

    Any feedback on the service is appreciated!

    Best,
    Morten L. Jørgensen

  12. patrick says:

    Hi There – Any idea how to add an exception for a single IP address? Any help would be so appreciated. I have the following code in my index.php file that redirects .com traffic from canada to an alternate .ca web address. Only trouble is that i need a single contact there (in Canada) to have access to my .com website as well.

  13. Hello sir i have been searching for geoip redirection since days and finally got your useful post i did everything exactly you said ,
    I am using cpanel which is similar to hostgator
    But i am getting this error ,
    You can also check on http://www.ebonyte.com/redirect.php (this is where i created the redirect which you said)
    please help me out, thanks.
    —-
    Warning: fopen(GeoIP.dat) [function.fopen]: failed to open stream: No such file or directory in /usr/local/lib/php/Net/GeoIP.php on line 425

    Fatal error: Uncaught PEAR_Exception: Unable to open file: GeoIP.dat in /usr/local/lib/php/Net/GeoIP.php on line 336 Exception trace #FunctionLocation 0Net_GeoIP->open(‘GeoIP.dat’, null)/usr/local/lib/php/Net/GeoIP.php:336 1Net_GeoIP->__construct(‘GeoIP.dat’, null)/usr/local/lib/php/Net/GeoIP.php:395 2Net_GeoIP::getInstance(‘GeoIP.dat’)/home/ebonyte/public_html/redirect.ph in /usr/local/lib/php/Net/GeoIP.php on line 427

  14. hey i am using opencart and want to use georedirect between my two stores ,i have used your method and it showing t his error ,please help me out thanks

    Warning: fopen(GeoIP.dat) [function.fopen]: failed to open stream: No such file or directory in /usr/local/lib/php/Net/GeoIP.php on line 425

    Fatal error: Uncaught PEAR_Exception: Unable to open file: GeoIP.dat in /usr/local/lib/php/Net/GeoIP.php on line 336 Exception trace #FunctionLocation 0Net_GeoIP->open(‘GeoIP.dat’, null)/usr/local/lib/php/Net/GeoIP.php:336 1Net_GeoIP->__construct(‘GeoIP.dat’, null)/usr/local/lib/php/Net/GeoIP.php:395 2Net_GeoIP::getInstance(‘GeoIP.dat’)/home/ebonyte/public_html/redirect.ph in /usr/local/lib/php/Net/GeoIP.php on line 427

  15. Thank you so much Howie for this great step by step and awesome tutorial i was searching for almost week but you page wasn’t appearing on google but it was showing useless posts. Keep it up…….. saved my cost :)

  16. i have problem with this.. when i use it, it make redirect loop

    my default url is .pk
    i placed case UK
    but if i open it in PK location it creates redirect loop
    how to solve this problem?

Trackbacks

  1. Quora says:

    Is there a WordPress plugin that makes it easy to change the site based on location?…

    You need GeoIP re-direct to sub-domain or domain. These tools below will help you create the re-direct, but you will still need to build out each site with unique content or dynamic tags and content. Paid WP Plug-in http://codecanyon.net/item/wordpress

Speak Your Mind

*


CommentLuv badge