2014-12-28 3 views
0

나는 Maxmind GeoIP2를 얼마 동안 샀고 국가를 기반으로 간단한 리디렉션을하고 싶지만 자신의 웹 사이트에서도이를 수행하는 방법이 명확하지 않습니다. 코딩에 능숙하지는 않지만 어떻게 작동하는지 이해하려고합니다 ...Maxmind를 사용하여 국가에 따라 트래픽을 리디렉션하는 방법 Geoip2 Javascript

예를 들어 제 웹 사이트는 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>

+0

당신이 봤어 [MaxMind의 재 튜토리얼] (http://dev.maxmind.com/geoip/geoip2/javascript/tutorial/#Redirect_Users_to_a_Country-Specific_Site)? –

답변

1

내가이 Maxmind Tutorial에 따라 좀 더 정교한 버전을 만들어

<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> 
0

작동한다,이보십시오. GeoIP2 자바 스크립트 클라이언트 API를 사용하려면 register your domain at Maxmind and purchase credits해야합니다 (위의 해결책과 동일)! 재미있어!

<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>