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

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

Problem Description:

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=">
<script type="text/javascript">
    window.location.href = ""

But I’m not able to redirect any page of the first domain towards the corresponding one of the second — say, to

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.

Solution – 1

Solved using JavaScript as follows:

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

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

Solution – 2

The easiest way to redirect to a different domain is to directly set the hostname (not host) part of the URL like this:

     window.location.hostname = ''

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:

Rate this post
We use cookies in order to give you the best possible experience on our website. By continuing to use this site, you agree to our use of cookies.