How to create redirects by country?

Asked

Viewed 664 times

3

How can I create redirects by country?

I mean, suppose I get a visitor from Mexico, then you’d have to redirect to meusite.com/mx/, already if I receive a visit from Brazil go to meusite.com/br/ or even if you receive a visit from Portugal redirect to meusite.com/pt/, etc....

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

1 answer

2


You can use some features.

When the browser has access to geolocation

You can use javascript to get the request geolocation. 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]);
        });
    }

Rely on the ip that made the request

There are companies that give you the exact location of the request based on ip, for example, the company: https://www.maxmind.com/

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

One of the ways to identify which language the user whether receive the content of the post (the answer) is evaluating the variable Accept-Language that comes in the request.

For example, in php it would be:

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

In this case, the user accepts (wishes to) receive the content in en-US. So, you know exactly where to redirect a post that comes from meusite.com/ for 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 the place he is: if he is in the United States - en-US, if he is in Brazil - en-BR. However, this is not a rule. Some users can configure their browsers en-US, 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, whereas 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" ) ;
  • Alexander the en means "I prefer English", the en-US means I accept any English language from the United States and the q=0.8 is called the relative quality factor (relative quality factor), and represents the estimation of user preference. More details on: http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.4

  • 1

    First: you don’t know how to express very well what you want. Have you seen how many questions you have within your question? Every question mark is a question. Second, I am not here to teach you anything. Every answer is a help. I am not your teacher! You should pay more attention to the comments you make and try to avoid being less rude to those who try to help you with a problem.

  • 1

    The answer is great, @Alexandrelopes; super complete, thanks Maia! If what you want is just copy/Paste, then the option to buy a script was very wise.

  • Now yes! Thank you!!!!!!!!!!!!!!!!!!!!!!!!!! Got better than the script! : D

  • 1

    Yes. The final code is in trouble. I put single quotes and should be double quotes to concatenate the $lang variable with the string. Fixed.

  • Now went to meusite.com/pt-BR,pt;q=0.8,en-US;q=0.6,en;q=0.4 should only be up to the first comma.

  • 1

    Then just treat the $lang string and get only the part you want. For example: $lang = split(",", $_SERVER['HTTP_ACCEPT_LANGUAGE'])[0];

  • This code above is wrong. You are a Genius!!!! and if I only want what is inside the second comma, in case: pt ? Sorry for so many questions. : 3

  • 1

    Use the next index. In this case, change the index [0] for [1]. If you don’t know how to handle string and handle array`s, I suggest you take a look at: http://www.php.net/manual/en/book.strings.php and http://www.php.net//manualpt_BR/book.array.php. It will help you to understand php better.

  • The more the code is wrong, Dreamweaver says it’s wrong, and when I open in the browser is only a white screen.

  • That part that’s wrong: $lang = split(",", $_SERVER['HTTP_ACCEPT_LANGUAGE'])[0];

Show 6 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.