问题
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