Reputation: 178
I wish to redirect a page from a domain (properly a subdomain) to another maintaining the same path component.
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 site 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 — say, 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 can only use HTML, JavaScript or PHP.
Upvotes: 4
Views: 5645
Reputation: 61
The easiest way to redirect to a different domain is to directly set the hostname (not host) part of the URL like this:
<script>
window.location.hostname = 'subdomain-2.example.com'
</script>
This will redirect you to the same URL (including protocol, port, query string, hash) but with the domain replaced.
Here is a nice visualization to understand all properties on the location object: https://developer.mozilla.org/en-US/docs/Web/API/Location#location_anatomy
Upvotes: 2