根据国家/地区重定向

时间:2014-11-18 06:24:53

标签: javascript html url url-redirection

我想在Github上托管一个不支持php的网页,然后将除中国之外的所有访问者重定向到另一个网站(在Weebly上托管)。

原因是Weebly在中国被封锁,但提供了更好的用户体验。

如果没有启用PhP,我该怎么做?有没有办法在Javascript中做到这一点?

4 个答案:

答案 0 :(得分:4)

您可以使用geoPlugin

首先添加

<script language="JavaScript" src="http://www.geoplugin.net/javascript.gp" type="text/javascript"></script>

到您的网页

然后尝试这个

&#13;
&#13;
document.write("Welcome to our visitors from "+geoplugin_countryName()); 
&#13;
 <script language="JavaScript" src="http://www.geoplugin.net/javascript.gp" type="text/javascript"></script>
 </head>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

您也可以使用ipinfo.io

var xmlhttp = new XMLHttpRequest();
xmlhttp.open('GET', '//ipinfo.io', true);
xmlhttp.onreadystatechange = function() {
    if (xmlhttp.readyState == 4) {
        if(xmlhttp.status == 200) {
            var obj = JSON.parse(xmlhttp.responseText);
            if (obj.country != 'CN')
                window.location.replace('http://www.weebly.com/...');
         }
    }
};
xmlhttp.send(null);

答案 2 :(得分:1)

document.write("Welcome to our visitors from "+geoplugin_countryName()); 
 <script language="JavaScript" src="http://www.geoplugin.net/javascript.gp" type="text/javascript"></script>
 </head>

答案 3 :(得分:0)

<script>
function geo(a) {
switch (a.country.code)
{
   case "SE": // Redirect is visitor from Sweden
   case "NO": // Redirect is visitor from Norway
   case "FR": // Redirect is visitor from FRANCE
   case "PL": // Redirect is visitor from POLAND
   case "GY": // Redirect is visitor from GERMANY
       window.location = "https://se.brixtol.com" + window.location.pathname; // edit for your URL
       break;
   default: 
       return null;
}
}
</script>enter code here`
<script src="https://geoip.nekudo.com/api?callback=geo"></script>
</pre>