giucal giucal - 4 months ago 17
HTML Question

How to redirect from a domain to another keeping the same path?

I've read lots of stuff here on SO, but it doesn't fit with my needs.

I wish to redirect a page from a domain (properly a subdomain) to another maintaining the same path.

I'm using a blogging service, not running a server; but I can edit the global head of my blog.

I've managed to redirect towards the main page of the other domain using:

<meta http-equiv="refresh" content="1; url=http://subdomain-2.domain.com">
<script type="text/javascript">
window.location.href = "http://subdomain-2.domain.com"
</script>


But I'm not able to redirect any page of the first domain towards the corresponding one of the second — for instance, http://subdomain-1.example.com/post/lorem-ipsum-123 to http://subdomain-2.example.com/post/lorem-ipsum-123.

I'm wondering if it's possible to grab the current URL and substitute the target domain to the original just by editing the head.

I suppose I could only use HTML, JavaScript or PHP.

Is there any way to achieve this?

Answer

Solved using JavaScript as follows:

<script type="text/javascript">
    window.location.href = "http://" + "here-your-target-domain" + window.location.pathname
</script>

The key element was window.location.pathname. Here I've found what I needed.