How to create country redirects?

3

How can I do to create country redirects?

I mean, let's suppose I get a visitor to Mexico , so I would have to redirect to meusite.com/mx/ , since if I get a visit from Brazil go to meusite.com/br/ or even if you receive a Portugal visit redirect to meusite.com/pt/ , etc ...

How can I do this? What is the best solution?

    
asked by anonymous 29.06.2014 / 21:37

1 answer

2

You can use some features.

When the browser has geolocation access

You can use javascript to get the geolocation of the request. However, you will have to know that longitude A and latitude B, is part of country X. I believe this will generate you some work:

if(navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(function(position) {
            var la = position.coords.latitude;
            var lo = position.coords.longitude; console.log([la, lo]);
        });
    }

Based on the ip that made the request

There are companies that give you the exact location of the request based on the ip, for example, the company: link

Deliver what the user wants to see (as the staff usually does)

One of the ways to identify which language the user wants to receive the content of the post is by evaluating the variable Accept-Language that comes in the request.

For example, in php would be:

<?php
echo $_SERVER['HTTP_ACCEPT_LANGUAGE']; 
// "en-US,en;q=0.8"

In this case, the user accepts (wants) to receive the content in en-US . So, you know exactly where to redirect a post that comes from meusite.com/ to meusite.com/en for example.

This is the most common way of identifying where the user comes from, because the user usually configures what he wants to receive according to where they are: if he is in the US - is in Brazil - en-GB. But this is not a rule. Some users can configure their en-US browsers, be in Brazil and ignore all other languages. So this way of identifying the language is not based on where the user is, but rather what the user wants to receive - which is much better in my understanding.

If the user is Japanese and is in Brazil, the ideal is to redirect him to meusite.com/jp , considering that this user will be more comfortable with his native language.

To redirect in PHP

$lang = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
header("Location: http://www.meusite.com/$lang" ) ;
    
29.06.2014 / 23:09