如何使用Maxmind Geoip2 Javascript根据国家/地区重定向流量
How To Redirect Traffic Based On Country Using Maxmind Geoip2 Javascript
我买了一段时间的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>
相关文章:
- Possibile使用Manifest v2下的HTML5地理位置API将lat/long转换为国家/地区
- 无法使用国家/地区选择中的状态动态填充下拉列表
- 使用自动完成服务()将 Google 地图地点限制为特定国家/地区
- Magento:根据所选国家/地区隐藏/显示输入字段
- 仅在引导程序表单助手国家/地区选取器中显示国家/地区子集文本
- 如何从特定国家/地区重定向用户
- 根据国家/地区更改货币
- 如何将 gmap 搜索自动完成功能限制为有限的国家/地区
- 带有标志和文本的国家/地区建议列表
- 选择国家/地区时更改颜色
- 相对于选定的地区中心缩放SVG国家/地区地图
- Three.js地球仪-使国家/地区可点击
- javascript从所选国家/地区值中选择城市
- 如何获得用户'的国家/地区,并转发到URL
- 下拉菜单,用于选择国家/地区,然后选择州
- 如果在JavaScript w/HTML中选择了特定国家/地区,如何要求邮政编码为数字
- 我如何限制谷歌地图API's的搜索框将搜索到一个国家/地区
- 如何制作验证电话号码中国家/地区代码的正则表达式
- 如何防止谷歌地理编码器从其他国家/地区返回结果
- 如何读取钛合金中的当地国家/地区电话代码xml