I have a website and would like to redirect visitors from different countries to an URL-Path. For this purpose, I use the code below. The problem with this code is that it always redirects you to the same URL, no matter what link you click on the page. What am I missing?
<!-- IP REDIRECTING -->
<script>
// Geolocation Redirect
let eng_list = ['BE', 'DK', 'FI', 'FR', 'IS', 'IT', 'HR', 'NL', 'RO', 'SK', 'HU', 'BG', 'GR', 'IE', 'GB', 'CA', 'LU', 'NO', 'PL', 'SE', 'RS', 'CZ', 'TR', 'US', 'AL', 'EE', 'LV', 'LT', 'MK', 'PT', 'SM', 'ES', 'IC', 'EA'];
let presentUrl = location.href;
let targetUrl = "https://www.homepage.de/en";
// Getting the country code from the user's IP
if (presentUrl != targetUrl) {
$.getJSON('https://ipinfo.io/json', function(data) {
if (eng_list.includes(data.country)) {
location.href = "https://www.homepage.de/en";
}
}, "jsonp");};
</script>
Your line in the if
always redirects to the English "subdomain" as you have it hard coded as such via this line:
location.href = "https://www.homepage.de/en";
Though this not really a "subdomain" but is besides the point. If you want to make it so different languages are actually applied then you need to remove the hardcoding to /en
and make that part "vary" based on the conditions needed.