Pregunta

I want to redirect my users to different languages/subfolders based on their IP address. To do this I use the JavaScript GeoIP API from MaxMind.

The problem: The english speaking people should stay at mydomain.com and not go to mydomain.com/en/. But when I redirect to mydomain.com the GeoIP script runs again which creates an infinite loop.

Here is my code (in index.html for mydomain.com):

<script language="JavaScript" src="http://j.maxmind.com/app/geoip.js"></script>
<script language="JavaScript">
 var country = geoip_country_code();
 if(country  == "FR")      
  {
  window.location = "http://mydomain.com/fr/"
  }
 else   
  {
  window.location = "http://mydomain.com/";
  }
</script>

In other posts I read about setting a cookie, but I wasn't able to do it in a way that solves the problem (and it would still create a loop when the user doesn't accept cookies, on mobile for example).

Another solution could be to redirect to mydomain.com/en/ and delete the /en/ folder in the URL via htaccess, but I wasn't able to get this done either.

An example of how I want it to work would be waze.com (it seems like they have the english version in the /en/ folder, but delete it from the URL).

So if anybody is able to help, I would be very grateful. Thanks a lot!

EDIT: I solved the problem myself. It's very simple: Just use the root directory for the english page and change function to "else {null;}" :-)

¿Fue útil?

Solución

Your problem is not with geoip but with your code.

Try this:

var country = geoip_country_code();
var currentLocation = String(window.location);

//if geoip is equal FR and window.location is different "http://mydomain.com/fr/"
if(country === "FR" && currentLocation.indexOf("http://mydomain.com/fr/")!==0)
{
    window.location = "http://mydomain.com/fr/"
}
//if geoip is different FR and window.location is equal "http://mydomain.com/fr/"
else if(currentLocation.indexOf("http://mydomain.com/fr/")===0)
{
    window.location = "http://mydomain.com/";
}

To detect using multiple languages ​​simply edit the following variables:

  1. var defaultsLang are the languages ​​that are supported by the main root (site.com/)

  2. var languages languages supported by sub-pages (site.com/fr/, site.com/es/, etc.)

See code (not tested):

(function(){
    var defaultsLang = ["en-us","en"];
    var languages = {
        "fr": true,     //enable french pages
        "pt": false,    //tmp disable portuguese pages
        "es": true      //enable spanish pages
    };
    var country = geoip_country_code().toLowerCase(),
    currentLocation = String(window.location),
    detectCurrent = function(){
        var a = currentLocation.replace(/^(http|https)[:]\/\//, "");
        var b = a.split("\/");
        b = b[1].toLowerCase();
        a = null;
        return b.length<5 && (/^[a-z\-]+$/).test(b) ? b : false;
    };

    var currentLang = detectCurrent();
        defaultsLang = "|"+defaultsLang.join("|")+"|";

        if(currentLang!==country && typeof languages[country] !=="undefined" && languages[country]!==false){
            window.location = "http://mydomain.com/" + country + "/";
        } else if(
            defaultsLang.indexOf("|" + currentLang + "|")===-1 && //current page is not the same as default languague(s)
            defaultsLang.indexOf("|" + country + "|")!==-1 && //geoip is in the list of default language(s)
            currentLang!==false
        ){
            window.location = "http://mydomain.com/";
        }
})();
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top