2

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
user1812111
  • 83
  • 1
  • 1
  • 11
  • There is surely an HTTP API you can use that does a lookup from IP to country? Have you looked at MaxMind? What programming language are you using? – halfer Jan 28 '18 at 11:08
  • Please read [Under what circumstances may I add “urgent” or other similar phrases to my question, in order to obtain faster answers?](//meta.stackoverflow.com/q/326569) - the summary is that this is not an ideal way to address volunteers, and is probably counterproductive to obtaining answers. Please refrain from adding this to your questions. – halfer Jan 28 '18 at 11:09
  • @halfer Javascript – user1812111 Jan 28 '18 at 11:17
  • Ah, just frontend? OK, do a search for "JavaScript country to IP" please, both here on Stack Overflow and on your favourite search engine. – halfer Jan 28 '18 at 11:18

1 Answers1

2

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";
    }
});
shoyd
  • 31
  • 3