CodeIgniter Forums
redirect domain based on country - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: External Resources (https://forum.codeigniter.com/forumdisplay.php?fid=7)
+--- Forum: Addins (https://forum.codeigniter.com/forumdisplay.php?fid=13)
+--- Thread: redirect domain based on country (/showthread.php?tid=72005)



redirect domain based on country - menilanjan - 10-24-2018

I am using hmvc in ci.How will I redirect domain based on country code?
I have modified index.php file and added checking whether this ip from a particular country then page is redirected to particular domain. Is it good practise in CI? or there is any other option. please suggest.


RE: redirect domain based on country - visualsol - 11-10-2018

There's not really any advantage I can think of to run this logic server-side.
I would just use javascript/jquery and then you just use an API to resolve the visitor country code by IP and redirect to the corresponding URL :

Code:
$.getJSON("http://api.db-ip.com/v2/free/self").then(function(addrInfo) {
  if (addrInfo.countryCode== "US") {
      document.location = "http://us.example.com";
  } else if (...) {
      // handle other cases
  } else {
      document.location = "http://example.com";
  }
});


Should you still want a server-side api call, here is a PHP library for the demonstrated api: https://github.com/dbip/api-client