I know that I can configure each domain specifically inside _redirects or netlify.toml but I would like to avoid having to make code changes and force a redeploy when a new domain is added.
I had a simple attempt at creating a rule via netlify.toml by attempting to use a named placeholder for domain:
from = "https://:domain/*"
to = "https://mysite.com/:splat"
status = 301
force = true
This didn’t work, instead showing an error during the build process:
Could not parse redirect number 1:
Invalid URL: Invalid URL
* Note: There are a few reasons for wanting to avoid modifying the redirects configuration every time a new domain is added, but the primary reason is that we avoid storing environment-level configuration in our source repositories.
The short answer here appears to be: You cannot use a completely wildcard domain redirect. The best you can currently achieve is to explicitly specify all known domain aliases (e.g. in an environment variable) and generate redirects into a _redirects file during your build process.
For example, I’m using Next.js. Prior to npm build I run a simple custom node module which reads an environment variable called DOMAIN_ALIASES which is a comma-separated list of domains which need to redirect all traffic to the matching path of the primary domain. I then read a template _redirects file, append the new redirects, and save the result to /public, e.g.