Redirect old domain to new one

Hello,

I have a primary domain, and I’d like to make a secondary domain as a redirect to the primary.
Currently they are both working as 2 separate domains, but I’d like to have only one primary and the other one redirecting.

How to do this in Netlify?

Thank you

This can be done by adding a 301 redirect rule:

https://docs.netlify.com/routing/redirects/redirect-options/#domain-level-redirects

If the two domains are original-site.org and second-domain-to-redirect.org, you can do this in _redirects format like so:

https://second-domain-to-redirect.org/*  https://original-site.org/:splat  301!
  • Note: that is 301! and not just 301 above - the exclamation mark is important and must be included

You can the same rule in netlify.toml format like this:

[[redirects]]
  from = "https://second-domain-to-redirect.org/*"
  to = "https://original-site.org/:splat"
  status = 301
  force = true

Just replace the domains above (original-site.org and second-domain-to-redirect.org) with the real domains for your site. Note, you may need to duplicate this redirect for the www subdomain like so:

https://second-domain-to-redirect.org/*  https://original-site.org/:splat  301!
https://www.second-domain-to-redirect.org/*  https://original-site.org/:splat  301!
1 Like

Hello Luke,

Thank you so much for your reply!
I have set a _redirects file with that rule you mentioned, but it didn’t work in my scenario…

One thing I forgot to mention, is that they are subdomains of the same domain. So the first domain is something like https://subfirst.original-site.org and the second domain is https://subsecond.original-site.org

Does being a subdomain from the same domain, may have something to do with it not working with the _redirects rules?

This is my _redirects right now:

https://subfirst.original-site.org/*  https://subsecond.original-site.org/:splat  301!

My build also worked without errors:

* 1 redirect rule processed
All redirect rules deployed without errors.

Thank you!

Hi, @podrivo. I believe I found the site (as no other sites on your account have any deploys on November. The current deploy’s _redirects syntax is wrong.

You said it looks like this (and this would work if this is how the rule was written - but it’s not):

https://subfirst.original-site.org/*  https://subsecond.original-site.org/:splat  301!

However, the actual rule in your _redirects file looks like this:

/subfirst.original-site./*  https://subsecond.original-site.org/:splat  301!

You did not include https:// in the starting URL. You started the URL with just / so it is being treated as a path and not as a URL. If you write the redirect rule as required, it will work.

1 Like

OMG, Luke! Thank you for pointing it out! I did the update in a different file…
It is really working this time as you mentioned, so thank you so much for your help! <3

1 Like