简体   繁体   English

我的重定向不适用于 IPInfo.io

[英]My redirect isn't working well with IPInfo.io

I need help with redirect to subdomains.我需要重定向到子域的帮助。 With IPinfo I get country of visitor then i redirect him to one of my subdomains and i need this look like ua.mysite or kz.mysite or ru.mysite I tried window.location.href = data.country + 'mysite' but this didn't work.使用 IPinfo 我得到访问者的国家/地区,然后我将他重定向到我的一个子域,我需要这个看起来像 ua.mysite 或 kz.mysite 或 ru.mysite 我试过window.location.href = data.country + 'mysite'但是这个没用。 Hope you can help me: :)希望你能帮我: :)

  // 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'
        }
    })
  }
}```

Updates to window.location.hostname don't cause redirect.window.location.hostname的更新不会导致重定向。

You should update window.location directly like this (note the "https:/"):您应该像这样直接更新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'
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM