简体   繁体   中英

Detect URL and Redirecting URL using Javascript

I have a question on how to detect a domain url using Javascript and redirect url.

My point is to redirect url if the url is not my domain. (Eg. my domain is website.com. if the domain is not website.com, it will redirect to website.com.)

I think this will help me against Httrack or other web copier. This will redirect their url if my file is on their site.

Thank in advance.

You can get the current DOMAIN (without any other paths etc)

window.location.hostname;

You can then 'redirect' to another URL using

// similar behavior as an HTTP redirect
window.location.replace("http://stackoverflow.com");

// similar behavior as clicking on a link
window.location.href = "http://stackoverflow.com";

// Another method for doing redirecting with JavaScript is this:
window.location = "https://stackoverflow.com";

Obviously you'll need to carry out a comparison against your own URL before you carry out the redirect, so:

if (window.location.hostname !== "www.mywebsite.com"){
  window.location = "https://www.mywebsite.com";
}

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