简体   繁体   中英

Javascript to detect domain and redirect

I'm hoping someone can help me, this HAS to be possible.

My work uses Shopify to run three ecommerce stores for three separate brands. To make things easier for many reasons operationally I have merged the three stores into one.

I need to redirect the existing brand domain names to the appropriate page in the new store. For example www.brandnameone.com.au needs to go to www.shopifystore.com.au/?view=brandnameone

What I'd like to do is detect which domain name the customer is coming from and redirect them.

Is anyone able to help me out with the script beyond

if(document.domain == "brandnameone.com.au")

?

Thanks so much!

You can do window.location = 'http://URL'; to redirect someone in javascript

This is pretty trivial. You can use window.location.hostname to get the hostname of the current page ( source ). and redirect using window.location.replace to redirect ( source ).

switch(window.location.hostname){
   case "brandnameone.com.au":
      window.location.replace("www.shopifystore.com.au/?view=brandnameone");
      break;
   ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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