0

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.

tillsanders
  • 1,656
  • 1
  • 17
  • 29
Gen Happy
  • 9
  • 1
  • 2
    Possible duplicates / related questions: https://stackoverflow.com/questions/6941533/get-protocol-domain-and-port-from-url https://stackoverflow.com/questions/10453306/how-can-i-protect-my-site-from-httrack-or-other-softwares-ripping https://stackoverflow.com/questions/503093/how-do-i-redirect-to-another-webpage Please conduct a few searches before opening a new question :) Also, I removed the `java` tag, since this is not Java related. – tillsanders Jun 04 '20 at 09:29

1 Answers1

0

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";
}