【问题标题】:Redirect based on country根据国家/地区重定向
【发布时间】:2014-11-18 06:24:53
【问题描述】:

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

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

在没有启用 PhP 的情况下如何做到这一点?有没有办法在 Javascript 中做到这一点?

【问题讨论】:

    标签: javascript html url url-redirection


    【解决方案1】:

    您可以使用geoPlugin

    先添加

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

    到你的网页

    那就试试这个

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

    【讨论】:

      【解决方案2】:

      你也可以使用 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);
      

      【讨论】:

      • 这需要 jquery 但他说的是 javascript
      • @AnikIslamAbhi 你是对的。我刚刚将其转换为纯 JavaScript。
      【解决方案3】:

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

      【讨论】:

        【解决方案4】:
        <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>
        

        【讨论】:

        • 请添加一点解释——不要只发布一个纯代码块
        猜你喜欢
        • 2019-04-15
        • 2018-07-06
        • 1970-01-01
        • 1970-01-01
        • 2020-02-27
        • 2012-04-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多