HowWhoWhen

Online Marketing Blog

  • Home
  • About
  • Contact

How to GEO Redirect Based on Country

March 5, 2012 By Howie Nguyen 7 Comments

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.

Filed Under: Coding Tagged With: CPA, geo redirect, PHP

Comments

  1. web hosting says

    March 5, 2012 at 8:32 PM

    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 =)

    Reply
  2. Howie Nguyen says

    March 29, 2012 at 11:15 AM

    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.

    Reply
  3. Mike says

    April 4, 2012 at 8:09 AM

    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?

    Reply
  4. Howie Nguyen says

    April 4, 2012 at 11:28 AM

    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.

    Reply
    • Mike says

      April 4, 2012 at 12:04 PM

      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.

      Reply
      • Howie Nguyen says

        April 4, 2012 at 12:19 PM

        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.

        Reply
        • Mike says

          April 4, 2012 at 1:06 PM

          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!

          Reply

Leave a Reply to web hosting Cancel reply

Your email address will not be published. Required fields are marked *

CommentLuv badgeShow more posts

(FREE) Marketing Funnel Pack

Professionally Designed Marketing Graphics

CONNECT WITH ME!

Follow @HowWhoWhen

Recent Posts

  • Are Your Affiliates Cheapening Your Brand?
  • Video Marketing Gone Wild?
  • Get the Website Traffic You’ve Always Dreamed Of
  • The Top Benefits of PPC Campaigns
  • How to Write the Perfect Blog Post

Email Marketing Tips

[footer_backtotop]

Copyright © 2025 · All Rights Reserved · Privacy Policy