I get occasional requests from colleagues to acquire domains for a particular project which act as a simple redirect. Any traffic to any version of custom-domain.tld gets forwarded to the same destination. I’d like to have a single Netlify site (with no actual site per se) handle this for multiple domains.
I’ve got this working for a single example right now - serene-austin-54062e.netlify.app - covering two variant domain names, any combination of http/https, with or without a subdomain, and with or without anything in the path. The end result is just a blank page and a bunch of redirects in a netlify.toml file. It’s a bit verbose. I’ve got the following repeated six times:
[[redirects]] from = "http://www.custom-domain.tld/*" to = "https://destination.tld/path" status = 301 force = true
The six times are for the variations mentioned above. In this case, that’s:
- http www domain 1
- https www domain 1
- http domain 2
- https domain 2
- http www domain 2
- https www domain 2.
(Note that it would be 8 times, but the primary domain redirects from domain.tld to www.domain.tld automatically, which is good enough for me in this case.)
I’d like to do that for several different domains and destinations, but at that point it’s starting to get unwieldy and tough to test. So that leads to a few questions, which I haven’t had the time or opportunity to try out yet:
- Can I use
http*://...to match both http and https?
- Can I use
https://*domain.tld/*to match with or without a subdomain?
- I couldn’t seem to get this working in the _redirects file and had to use toml. Is there any trick to using a _redirects file in a “site” with no build command or public folder?
- Is there any concern over generating a
netlify.tomlfile with a build script? I wouldn’t mind a verbose file if it were generated from simpler source files with one line per domain. I’m guessing _redirects might work while netlify.toml is presumably won’t, since it’s probably read before the build kicks off. Is that correct?
It would be so much clearer and less error prone to have one line per domain instead of 20. I could script it and commit the results, but it seems cleaner to just commit the source files, if possible.