Automatic redirect based on location
Autor: Craig K.
Visitado 1587,
Seguidores 1,
Compartilhado 0
How do you direct visitors automatically to the right page based on their geographic location (I'm interested in country)?
Publicado em
Hello Craig,
Have a look at this article in the official WebSite X5 Guide:
How to set up an automatic redirection according to the detected Browser language
Redirection based on the language employed by the browser is the closest you will get to a built-in solution for geographic location.
Otherwise you would have to employ custom programming based on IP address, and this has its own set of drawbacks.
Kind regards,
Paul
Search the WebSite X5 Help Center
Autor
Okay thanks, unfortunately language won't be able to distinguish between the countries I'm interested in as they all speak English.
https://stackoverflow.com/questions/9838344/how-to-redirect-domain-according-to-country-ip-address
Autor
Thanks, looks good, will give it a try.
Autor
Here is another using Javascript, which is client side, so possibly easier to implement. One problem with this (and your suggestion) they both rely on APIs which is probably slower than solutions that install a database on the server (such as IP2location).
https://gist.github.com/ipapi-co/b28ac4efaf8aa3e8596d202f7edc3586
(It > En) ... if you know the browser acronyms of the EN country languages you are interested in, for example [en-us] | [en-au], then you can condition this is my exclusive code, CLIENT (browser) side, (no API)!
... as it is now, it works with my browsers in IT, Chrome, Firefox, Internet Explorer ...
the languages IT, FR, EN, must be in the sub-directories it/, fr/, en/
... my original code, x INDEX - HEAD:
<script>
window.onload=function(){
var linguaK = navigator.languages && navigator.languages[0] || navigator.language || navigator.userLanguage;
linguaK = linguaK.toLowerCase();
if (linguaK.indexOf("it") != -1) {
location.href = "it/index.html";}
else if (linguaK.indexOf("fr") != -1) {
location.href = "fr/index.html";}
else if (linguaK.indexOf("en") != -1) {
location.href = "en/index.html";}
}
</script>
.
ciao
.
Autor
Thanks. I wonder if this would also be possible using Websitex5's built-in entry page feature with automatic language detection... or doesn't it go as far looking at regional variations of languages - like US vs UK vs SA english? Also, I looked at my browser and it is not set correctly, it is set automatically to US for some reason in Chrome.
... I currently cannot try, I am without PC and without program, but my code is simple and in theory it should be valid by conditioning correctly for the languages of interest, by modifying the code ...
... do this TEST and tell me the result you get with the various keyboards, US, UK, etc., maybe attach the various STAMP/screenshots...
... put this code in an HTML Code Object:
<script>
linguaK = navigator.languages && navigator.languages[0] || navigator.language || navigator.userLanguage;
alert(linguaK);
</script>
.
ciao
.
Hello KolAsim,
Here your detection is based on navigator values and user preference.
But you can use a navigator in english and be based into japan or china or brasil . So the detection is not based in your case on the location of the user !
I missed something
Axel
Hello Craig
Instead of the language you could find the country code by the IP.
You could try this :
var country_code = null; $.getJSON('http://ipinfo.io/' + userip, function(data){ country_code = data.country; alert(country_code); });
Some information here : https://stackoverflow.com/questions/673905/best-way-to-determine-users-locale-within-browser
And here : https://stackoverflow.com/questions/17680413/get-visitors-language-country-code-with-javascript-client-side
... as I said before, simply in a simple JS browser; ... no PHP, no API, no .htaccess, ... no complications ...
... ... on the net, as we have seen, there are many other solutions for all tastes, but not so immediate ...
... ciao...
.