2013-02-26 77 views
0

我想根據用戶國家重定向我的網站。例如,如果用戶從美國輸入www.domain.com,那麼他應該重定向到en.domain.com。根據用戶國家重定向網站

+1

[你有什麼試過嗎?](http://whathaveyoutried.com) – Dunhamzzz 2013-02-26 11:32:25

+0

什麼問題? – m0skit0 2013-02-26 11:32:51

+0

我希望美國用戶使用英文查看我的網站,德國用戶只要鍵入主域網址即可用德文查看。根據那裏的國家意思是自動重定向到那裏的子域。 – Akaash 2013-02-26 11:36:08

回答

0

好了,讓你開始,這裏是你如何可以檢測一個人的國家,使用HTML5地理定位:

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