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

↘锁芯ラ 提交于 2019-12-11 02:07:35

问题


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:


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";
    }
});


来源:https://stackoverflow.com/questions/48484957/redirect-main-website-to-sub-domain-based-on-visitor-ip-address-country

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!