1

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?

1
  • 5
    Shouldn't this be done at the server level ? Commented Nov 19, 2013 at 16:43

4 Answers 4

1

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');

Sign up to request clarification or add additional context in comments.

1 Comment

Yep, or even 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/…
1

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

4 Comments

Should it be location.href = url?
without the protocol it will redirect to a relative url
Well I think @xybrek should be able to handle it from here. No need to spell it out any further.
And I agree, it would be more appropriate to handle this server-side, but lets assume that he is limited to using client-only options.
0

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

3 Comments

.pathname doesn't include query string or hash/fragment
location.search only includes the query string, not the hash/fragment; you also need location.hash :P
I can't even find window.*redirect*. What environment are you in?
0
window.location.replace("http://www.other-domain.com/abc");

See Window.location - Web API interfaces | MDN for more info.

4 Comments

Why the down-vote? Depending on the situation window.location.replace can be better than setting window.location.href.
i didn't downvote but i imagine it is because you just showed replacing it with a hardcoded value, without passing on the current path, query or hash strings
Ok. I just used it as an example. The question wasn't very clear IMHO. I just assumed everybody knows how to concatenate a string. Oh well...
+1 to correct your -1. It is a valid answer to a rather vague question.

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.