How to redirect one site to another while maintaining the rest of the url

Asked

Viewed 105 times

1

I have a website meusite.net, but migrated to the realm meusite.com.br. However google has already indexed the more than 2000 pages I have on meusite.net. To solve this problem I created another page to use the domain meusite.net with the following redirect code:

<script>
  location.href="https://www.meusite.com.br/";
</script>

But this code does not solve totally because redirects all indexed pages to meusite.com.br and I want for example on the link meusite.net/contact.html it automatically changes to meusite.com.br/contact.html. Keeping the rest of the url.

  • tried to get the url before? var url = "https://www.meusite.com.br/" location.href= url + 'contato.html' ?

  • I had posted a response with a redirect code var url = location.href.split("/"); location.href = "https://meusite.com.br/"+url.pop();, but since I don’t know how Google will treat indexed pages with this redirect, I preferred to delete the answer.

  • I tested it. But this happens: the indexed page meusite.net/licenca/free.html redirects to meusite.com.br/free.html. Then it gives error 404 because it is not the complete link.

1 answer

3


You could use replace method. Example:

var url = location.href;
location.href = url.replace("meusite.net","meusite.com.br");

I hope to help you.

  • 1

    Yes, thank you. That’s right.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.