Monday 18 May 2015

MaxMinds GeoIp could be the solution for you if you want to run country specific redirects.



First off you’ll need to get a License Key.  

In your index.php ( or sub category etc that you want to redirect from )


At the top of this page call the JavaScript file from MaxMind . 


First of all lets make a variable called ‘redirect’ and implement the redirection 

var redirect = (function () {


Next up lets build the URL . To do this we make a variable call ‘redirectBrowser’  and inside that pass a Site variable to build the uri . The site variable will be assigned once we’ve made decision on which countries IP we are passing onto.   Here’s that variable and it’s function. 

var redirectBrowser = function (site) {
  var uri = "http://" + site;
  window.location = uri;
};

The variable ‘sites’ which is different to ‘site’  is an array of all the country codes we’ll be using.   You can find a list of the ‘Maxmind Country Codes’ here. 


In the following code I assign Nordic IPs and GB.  

var sites = {
  "no": true,
  "se": true,
  "gb": true,
  "dk": true,
  "fi": true
};

Change these to Lower Case

var code = geoipResponse.country.iso_code.toLowerCase();

Let’s have a look at this example .  As long as the sites are listed in the county codes then if they have no redirect here then they’ll stay on the existing website.  Otherwise they’ll be forwarded to the ‘else’ redirect. 

if ( sites[code] ) {

  if (code == 'gb'){
    var site = “theukwebsite.co.uk"
      redirectBrowser(site);
  }
}
else {
  redirectBrowser(“theworldwidewebwebsite.com");
}

So eventhough we don’t mention the Nordic countries here they will still be dealt with differently to countries that aren’t listed in the ‘sites’ variable. 

Let’s add onError forwarding also.  So here’s the full code. 

var redirect = (function () {
  /* This implements the actual redirection. */
  var redirectBrowser = function (site) {
    var uri = "http://" + site;
    window.location = uri;
  };

  /* These are the country codes for the countries we have sites for.
   * We will check to see if a visitor is coming from one of these countries.
   * If they are, we redirect them to the country-specific site. If not, we
   * redirect them to world.example.com */
  var sites = {
    "no": true,
    "se": true,
    "gb": true,
    "dk": true,
    "fi": true
  };
  var defaultSite = "gb";

  var onSuccess = function (geoipResponse) {
    /* There's no guarantee that a successful response object
     * has any particular property, so we need to code defensively. */
    if (!geoipResponse.country.iso_code) {
      redirectBrowser("gb");
      return;
    }

    /* ISO country codes are in upper case. */
    var code = geoipResponse.country.iso_code.toLowerCase();

    if ( sites[code] ) {

  if (code == 'gb'){
    var site = “theukwebsite.co.uk"
      redirectBrowser(site);
  }
}
else {
  redirectBrowser(“theworldwidewebwebsite.com");
}

  /* We don't really care what the error is, we'll send them
   * to the default site. */
  var onError = function (error) {
    redirectBrowser("theworldwidewebwebsite.com
");
  };

  return function () {
    geoip2.country( onSuccess, onError );
  };
}());

redirect();

No comments: