Redirecting domains using Netlify

10/6/2020

I recently decided to move one of my projects to a new domain. Since the old domain was on Namecheap, I thought about using the BasicDNS service provided by Namecheap. I had faced issues with Namecheap DNS in the past and the basic version did not seem to offer any uptime guarantees. Since the project was hosted on Netlify, using Netlify DNS was a no brainer. I just needed to configure Netlify DNS to redirect requests from the old domain to the new domain.

This took some more digging than I thought!. I am writing this down so it will help me in the future or you. First, I added the domain to Netlify using the UI and updated the DNS settings on Namecheap to point to Netlify’s DNS servers. Then I needed to setup a wildcard redirect on Netlify. This was not exactly straightforward.

Netlify lets you configure redirects using a _redirects file or using netlify.toml. I decided to use an _redirects file. I created a new git repository with an empty _redirects file and pushed that to Github. Then I linked my old domain to this repo using the UI.

https://old.com https://new.com 301!
https://old.com/* https://new.com/:splat 301!

Adding the lines above to the _redirects file and redeploying did the trick. All the links to the old domain were being redirected successfully to the new domain and the universe became a better place, atleast in terms of DNS resolution.