根据访问者的IP地址(国家/地区)将主网站重定向到子域 [英] Redirect main website to sub domain based on visitor IP address (country)

查看:159
本文介绍了根据访问者的IP地址(国家/地区)将主网站重定向到子域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要帮助,根据访问者的IP地址(位置)将我的网站重定向到子域.我有3个特定的网站

I need help redirecting my website to sub domain based on visitor ip address (location). I have 3 specific websites

  • 我想将来自欧洲的访客重定向到eu.mysite.com
  • 我想将来自美国的访客重定向到us.mysite.com
  • 我想将访客从世界其他地方重定向到mysite.com
  • 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

我尝试了一些代码并修改了htaccess,但由于未在服务器上安装GeoIp,所以没有帮助.

I tried several codes and modifying htaccess as well, it didn't help as GeoIp not installed on the server.

推荐答案

您只需使用API​​即可通过IP解析访问者大洲,然后重定向到相应的URL:

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";
    }
});

这篇关于根据访问者的IP地址(国家/地区)将主网站重定向到子域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆