-3

I want the page to redirect to https://example.com if the domain is not on either https://example.com or https://example.site or https://example.page. So far I tried this:

if(location.host != "example.com", "example.site", "example.dev") {
    window.location.replace("https://example.com");
}

1 Answers1

1

This can be done with indexOf() method, like this:

const hosts = [
  "example.com",
  "example.site",
  "example.dev",
  "stacksnippets.net" // This website
];

if (hosts.indexOf(location.host) > -1) {
  // Redirect
  console.log('redirect');
}
Samball
  • 623
  • 7
  • 22