Domain wildcard redirect

Is it possible to redirect any bound domain to the primary domain without having to explicitly specify the source domain?

For example, a client may purchase many domains for SEO purposes all of which are intended to redirect to the primary domain. E.g.

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:

[[redirects]]
  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:
  {"from":"https://:domain/*","to":"https://mysite.com/:splat","status":301,"force":true}
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.

Hi @lance-h :wave:t6:, welcome to the forums and thanks so much for reaching out.

There are some prerequisites & caveats you must satify for wildcard enablement…

  1. Pro level account or higher
  2. Your custom domain must be a subdomain - you use beam-commerce.com ; it would need to be www.beam-commerce.com to activate that feature
  3. domain aliases won’t work (those may all be covered by the wildcard, though, so I can remove them while configuring the site if you take care of #2
  4. branch subdomains won’t work on that site,

Finally please read this doc here: Delegate a stand-alone subdomain to Netlify DNS | Netlify Docs this should hopefully answer any outstanding questions.

Thanks @SamO.

I’ve created a sub-domain and delegated it to Netlify, however I don’t understand what you mean by #3. Are you saying that Netlify support can change something for me?

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.

const domainAliases = process.env.DOMAIN_ALIASES?.split(',') || [];
const aliasRedirects = domainAliases.map(host => `https://${host}/* ${process.env.URL}/:splat 301!`)

const template = await fs.readFile('./public/_redirects.template', 'utf-8');
const redirects = `${template}\n# Domain alias redirects\n${aliasRedirects.join('\n')}`;

await fs.writeFile('./public/_redirects', redirects, 'utf-8');

Note: You can’t dynamically add [[redirects]] to netlify.toml because the file appears to be entirely processed before your build command.

You can modify it using Build plugins:

1 Like