0

I want to put in my HTML/PHP a script for redirection if the website name does not match my word. I tried to use this code, but it doesn't work.

WITH REGEX

if (window.location.hostname !== 'myword.org'){
    window.top.location.href = 'http://redirecttomysite.org'; 
}

var website = window.location.hostname;
var internalLinkRegex = new RegExp('^((((http:\\/\\/|https:\\/\\/)(www\\.)?)?'
                                     + website
                                     + ')|(localhost:\\d{4})|(\\/.*))(\\/.*)?$', '');

WITHOUT REGEX

if (website !== 'myword.org'){
    window.top.location.href = 'http://redirecttomysite.org/forum'; 
}
2
  • How exactly does it not work? What are you trying to accomplish? Commented May 12, 2017 at 17:37
  • I removed the PHP and HTML tags, as this appears to be dealing strictly with JavaScript. The fact that it's in an HTML page generated by PHP isn't directly relevant. Commented May 12, 2017 at 17:40

3 Answers 3

1

try

if (website.indexOf('myword.org') == -1){
    window.top.location.href = 'http://redirecttomysite.org/forum'; 
}
Sign up to request clarification or add additional context in comments.

Comments

1
if(window.location.href.indexOf("myword.org") == -1) {
       window.location = 'http://redirecttomysite.org';
    }

Comments

0

simply as below

if (!(/stackoverflow\.com/i.test(location.hostname))){
...

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.