How do I redirect a request with Javascript with path?
my-domain.com/abc to www.other-domain.com/abc
Where 'abc' would be the request path. Also it is possible to also forward query parameters and fragment identifiers?
How do I redirect a request with Javascript with path?
my-domain.com/abc to www.other-domain.com/abc
Where 'abc' would be the request path. Also it is possible to also forward query parameters and fragment identifiers?
this should be done server-side but if you want to do it with javascript, you can do
location.href = location.href.replace(/(https?:\/\/)[^/]+/,'$1'+'www.other-domain.com');
note: that regex includes forwarding with the same protocol as the url.. if you do not include the protocol, it will attempt to redirect to the same domain as the page, but with 'www.other-domain.com/..' as the relative path. So you must include the protocol. As an alternative, you can just hardcode it like so:
location.href = location.href.replace(/(https?:\/\/)[^/]+/,'http://www.other-domain.com');
location.host = location.host.replace(/\.de/, ".com") for just replacing in the host component. query parameters do get preserved. But I agree with stackoverflow.com/questions/20077103/…Something like:
var url = "http://www.other-domain.com" + location.path + location.search + location.hash;
location.href = url;
For more info: http://www.w3schools.com/jsref/obj_location.asp
location.href = url?This should solve your problem, but it's highly recommended to be done at server level.
window.location = 'www.other-domain.com' + (location.pathname + location.search);
.pathname doesn't include query string or hash/fragmentlocation.search only includes the query string, not the hash/fragment; you also need location.hash :Pwindow.location.replace("http://www.other-domain.com/abc");
See Window.location - Web API interfaces | MDN for more info.
window.location.replace can be better than setting window.location.href.