Redirect a whole domain to another from your _redirects file

AddedCDNv0.0.132

TL;DR: A _redirects rule whose source is a full URL, such as https://old.example.com/* to https://new.example.com/:splat, now redirects that whole domain to the new one, as long as the old domain is claimed and bound to your site.

When a site moves to a new domain, links to the old one should land on the new one. Until now, a _redirects rule whose source was a whole URL was skipped. Now it works, in the same format Netlify uses:

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

Before a rule works

  • The old domain must be claimed and bound to your site. Your site’s rules only see requests for domains bound to it, so a rule can never redirect a domain you haven’t proved is yours.
  • The build lists the domains your rules need. It can’t see which domains are bound, so it names them instead: _redirects: domain-level rules for old.example.com; each fires only once that domain is claimed and bound to this site.

How rules are matched

  • Domain rules come before path rules, wherever they are in the file. It differs for those of you migrating from Netlify which uses file order: a catch-all such as /* /index.html 200 above a domain rule redirects here, but rewrites on Netlify.
  • The scheme counts. An http:// rule matches plain HTTP only. Visitors are moved to HTTPS first, so an http:// rule on its own never fires, and the build warns about it. Write https:// rules.
  • Case, port and a trailing dot in the visitor’s address are ignored.
  • Only 301 and 302. A domain rule can’t rewrite (200) or proxy. The build refuses a domain rule with 200 or 404.
  • Rules that would loop are skipped, such as a rule that sends a domain back to itself. Domain rules that send visitors round in a ring, such as www to the bare domain and back, stop the build.

Limits

  • Level 0 sites only, like everything in _redirects.
  • Later deploys don’t move the old domain. It stays bound to the deploy it was bound to. Its redirects keep working while that deploy’s rule is right, but changing the rule means binding the domain again.
  • A rule for a domain that isn’t bound isn’t refused. The domain may be bound after the deploy, so the build names it rather than stopping.

What you need to do

Nothing, unless you’re moving a domain. To redirect one, add a rule like the ones above to _redirects, claim the old domain and bind it to your site, then deploy.

← All changes · More CDN changes · Subscribe to CDN

View as Markdown