Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript redirect to same folder on different domain?

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!

like image 916
richoffrails Avatar asked Oct 25 '11 00:10

richoffrails


2 Answers

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.

like image 156
Dominic Barnes Avatar answered Oct 22 '22 10:10

Dominic Barnes


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.

like image 21
alex Avatar answered Oct 22 '22 11:10

alex