I'm trying to redirect users from one site to another but keep the part after the domain name the same. For example:
http://www.mysite.com/this-is-a-blog-post.html
to
http://www.newwebsite.com/this-is-a-blog-post.html
Can this be done using a javascript redirect? If so, what would the code look like? I appreciate your help!
Check out the window.location
object. If you just want to change the domain (keeping the same path) use this code.
window.location = "http://newwebsite.com/" + window.location.pathname;
If you want to do anything more sophisticated, you can mess with the window.location.pathname
value and use that to redirect.
To change just the domain and leave everything else (including protocol, port, query string, hash, etc), I'd use...
window.location.hostname = 'example.com';
jsFiddle.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With