如何使用Maxmind Geoip2 Javascript根据国家/地区重定向流量

How To Redirect Traffic Based On Country Using Maxmind Geoip2 Javascript

本文关键字:国家 地区 重定向 流量 何使用 Maxmind Geoip2 Javascript      更新时间:2023-09-26

我买了一段时间的Maxmind GeoIP2,我想根据国家/地区进行简单的重定向,但即使在他们的网站上似乎也没有明确的答案。我不擅长编码,但我试图了解它是如何工作的......

例如,我的网站 google.com,我想将一些国家重定向到 yahoo.com,我想出了这个代码,但它将每个人都重定向到 yahoo.com。任何人都可以帮助我或重建此脚本以使其达到其目的吗?多谢。。

<script src="//js.maxmind.com/js/apis/geoip2/v2.1/geoip2.js" type="text/javascript"></script>
<script language="JavaScript">
var user_country = geoip2.country(
    function (response) {
if (response.country.iso_code === "FR"){window.location = "http://yahoo.com"}
else {window.location = "http://google.com"}
if (response.country.iso_code === "BE"){window.location = "http://yahoo.com"}
else {window.location = "http://google.com"}
if (response.country.iso_code === "PH"){window.location = "http://yahoo.com"}
else {window.location = "http://google.com"}
    },
    function (error) {
        // handle error
    }
);
</script>

我基于

这个Maxmind教程创建了一个更复杂的版本。您需要在Maxmind注册您的域名并购买积分才能使用GeoIP2 JavaScript Client API(上述解决方案也是如此!玩得愉快!

<script src="//js.maxmind.com/js/apis/geoip2/v2.1/geoip2.js" type="text/javascript"></script>
<script language="JavaScript">
var redirect = (function () {
    /* Get actual URL */
    var url = window.location.href; 
    /* This implements the actual redirection. */
    var redirectBrowser = function (site) {
        var uri = "https://www.example.com/" + site;
        window.location = uri;
    };
    /* These are the country codes for the countries we have sites for.
     * We will check to see if a visitor is coming from one of these countries.
     * If they are, we redirect them to the country-specific site. If not, we
     * redirect them to https://www.example.com/ */
    var sites = {
        "us": {"active": true, "target": "en-us"},
        "gb": {"active": true, "target": "en-gb"},
        "de": {"active": true, "target": "de"},
        "fr": {"active": true, "target": "fr"},
        "fi": {"active": true, "target": "fi"},
        "hu": {"active": true, "target": "hu"},
        "nl": {"active": true, "target": "nl"},
        "se": {"active": true, "target": "sv"}
    };
    var defaultSite = "";
    var onSuccess = function (geoipResponse) {
        /* There's no guarantee that a successful response object
         * has any particular property, so we need to code defensively. */
        if (!geoipResponse.country.iso_code) {
            redirectBrowser(defaultSite);
            return;
        }
        /* ISO country codes are in upper case. */
        var code = geoipResponse.country.iso_code.toLowerCase();
        if ( sites[code].active ) {
            redirectBrowser(sites[code].target);
        }
        else if ( url == defaultSite ) {
            return;
        }
        else {
            redirectBrowser(defaultSite);
        }
    };
    /* We don't really care what the error is, we'll send them
     * to the default site. */
    var onError = function (error) {
        redirectBrowser(defaultSite);
    };
    return function () {
        geoip2.country( onSuccess, onError );
    };
}());
redirect();
</script>

试试这个,它应该可以工作

<script src="//js.maxmind.com/js/apis/geoip2/v2.1/geoip2.js" type="text/javascript"></script>
<script language="JavaScript">
geoip2.country(
    function (response) {
        if (response.country.iso_code == "FR") {
            window.location = "http://yahoo.com"
        }
        if (response.country.iso_code == "BE") {
            window.location = "http://yahoo.com"
        }
        if (response.country.iso_code == "PH") {
            window.location = "http://yahoo.com"
        }
        else {
            window.location = "http://google.com"
        }
    }
);
</script>