reneroth.xyz

Netlify Catchall Domain Redirect

· 2 min

Imagine you have one Netlify app that should be reachable under multiple domains, but every domain except the canonical one should redirect to the canonical one.

You could either set up the redirects externally, but that requires another domain management service. Unfortunately, Netlify does not allow us to set redirects for domain aliases in the webpanel.

The solution is to use redirect rules in netlify.toml.

A simple redirect rule would look like this:

[[redirects]]
  force = true
  from = "https://your-alias-domain/*"
  status = 301
  to = "https://your-canonical-domain/:splat"

Going line by line:

  • force = true - Forces the redirect even if the requested file exists
  • from = "https://your-alias-domain/*" - The domain alias to redirect from, the * being a wildcard to capture the requested path
  • status = 301 - Permanent redirect status code
  • to = "https://your-canonical-domain/:splat" - Canonical domain to redirect to, where :splat re-inserts the captured path

You would need to add this for every alias domain you want to redirect. Forget one, and suddenly your website is reachable under that domain too, which is bad for SEO.

There is a way to do this more elegantly using a catchall redirect:

[[redirects]]
  force = false
  from = "https://your-canonical-domain/*"
  status = 200
  to = "https://your-canonical-domain/:splat"

[[redirects]]
  force = true
  from = "/*"
  status = 301
  to = "https://your-canonical-domain/:splat"

This catches all requests to any domain and redirects them to the canonical domain.

It works because the first redirect rule matches the canonical domain. Netlify stops processing redirect rules once a match is found; otherwise the second rule would lead to an infinite loop once we’re on the canonical domain.

comment by email →