Redirect main website to sub domain based on visitor IP address (country)

2.3k views Asked by At

I need help redirecting my website to sub domain based on visitor ip address (location). I have 3 specific websites

  • I want to redirect visitors from Europe to eu.mysite.com
  • I want to redirect visitors from USA to us.mysite.com
  • I want to redirect visitors from rest of the world to mysite.com

I tried several codes and modifying htaccess as well, it didn't help as GeoIp not installed on the server.

1

There are 1 answers

0
shoyd On

You just use an API to resolve the visitor continent by IP and redirect to the corresponding URL :

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