我的重定向不能很好地处理 IPInfo.io



我需要重定向到子域的帮助。有了IPinfo,我得到了访问者的国家,然后我将他重定向到我的一个子域,我需要这个看起来像ua.mysite或kz.mysite或者ru.mysite我尝试了window.location.href = data.country + 'mysite',但没有成功。希望你能帮助我!:(

// Let's check if we have the value in localstorage
if (localStorage.getItem('country') == 'RU') {
// Already have the value in localStorage no need to make call to IPinfo
window.location.hostname = 'ru.mysite.tech'
} else if(localStorage.getItem('country') == 'KZ') {
// Already have the value in localStorage no need to make call to IPinfo
window.location.hostname = 'kz.mysite.tech'
} else if(localStorage.getItem('country') == 'UA') {
// Already have the value in localStorage no need to make call to IPinfo
window.location.hostname = 'ua.mysite.tech'
}
else{
// No cached data, let's get it from IPinfo
fetch('https://ipinfo.io/json?<MyToken>')
.then(res => res.json())
.then(data => {
if(data.country == 'UA'){
localStorage.setItem('ipinfo', data.country)
window.location.hostname = 'ua.mysite.tech'
}
else if(data.country == 'KZ'){
localStorage.setItem('ipinfo', data.country)
window.location.hostname = 'kz.mysite.tech'
}
else {
localStorage.setItem('ipinfo', data.country)
window.location.hostname = 'ru.mysite.tech'
}
})
}
}```

window.location.hostname的更新不会导致重定向。

您应该像这样直接更新window.location(注意"https://"(:

if (localStorage.getItem('country') == 'RU') {
// Already have the value in localStorage no need to make call to IPinfo
window.location = 'https://ru.mysite.tech'
}