skip to Main Content

We have an old domain on an old shopify site. We have a new domain on a new wordpress website.

Our goal is to re-direct all old links from the old site/domain to the new site domain.

For example if a user goes to www.old-website.com/collection/product

it will re-direct them to www.new-website.com/collection/product.

So basically i wana keep /collection/product but swap out the rest of the domain.

I am using this script but i think its too simple and just re-directing to the home page.

<script type="text/javascript">
            window.location.href = "https://new-website.com"
</script>

So instead of simply changing the location to the new domain. How do I keep the directories but change the domain and then re-direct?

3

Answers


  1. You can take the entire url (document.location.href) and split the parameters to append it to the new url.

    <script type="text/javascript">
            var restOfTheUrl = domainName.split('old-website.com')[1] || ''; 
            window.location.href = "https://new-website.com" + restOfTheUrl;
    </script>
    

    If your user will go to www.old-website.com/collection/product

    The entire path (except old domain) is appended to the new domain and the result url it will look like this https://new-website.com/collection/product

    Login or Signup to reply.
  2. If you are forced to do it with JavaScript, then simply try

    location.hostname = 'new-website.com';
    
    Login or Signup to reply.
  3. Thinking a little out of the box. If you have access to the DNS, you can easily do that using use the redirect.center service.

    You need to add / modify 2 records of the old-website.com zone:


    @   A     54.84.55.102
    www CNAME new-website.com
    

    The advantage is that this is a 301 redirect which is better for SEO. Also you can remove your old website, possibly saving some bucks.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search