【发布时间】:2013-02-26 11:31:26
【问题描述】:
我想根据用户所在的国家/地区重定向我的网站。例如,如果用户从美国输入 www.domain.com,那么他应该重定向到 en.domain.com。
【问题讨论】:
-
我希望美国用户在输入主域 URL 后以英语查看我的网站,德国用户以德语查看我的网站。意味着根据那里的国家自动重定向到那里的子域。
标签: javascript cakephp
我想根据用户所在的国家/地区重定向我的网站。例如,如果用户从美国输入 www.domain.com,那么他应该重定向到 en.domain.com。
【问题讨论】:
标签: javascript cakephp
首先,让您开始使用 HTML5 Geolocation 检测一个人的国家/地区:
var geocoder = new google.maps.Geocoder();
geocoder.geocode({'latLng': <YOURLATLNGRESPONSEFROMGEO>}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[0]) {
var loc = getCountry(results);
}
}
});
function getCountry(results)
{
for (var i = 0; i < results[0].address_components.length; i++)
{
var shortname = results[0].address_components[i].short_name;
var longname = results[0].address_components[i].long_name;
var type = results[0].address_components[i].types;
if (type.indexOf("country") != -1)
{
if (!isNullOrWhitespace(shortname))
{
return shortname;
}
else
{
return longname;
}
}
}
}
function isNullOrWhitespace(text) {
if (text == null) {
return true;
}
return text.replace(/\s/gi, '').length < 1;
}
这将得到这个人的国家。不幸的是,据我所知,没有办法使用它来重定向一个人。试试这个链接:http://dev.maxmind.com/geoip/geolite。
【讨论】:
【讨论】: