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 _redirects or netlify.toml file 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.
_redirects go into the directory that is published, there is no requirement that it has a specific name (e.g. public) and no requirement for a build command.
Have done this myself. As long as the formatting is correct no issues (that I have found).
Redirects are part of post-processing (post-build.)
Of course, as you don’t appear to wish to mention the domains you are using/testing it is impossible for anyone to test the behaviour for you. If you wish to have serene-austin-54062e.netlify.app redirected elsewhere, you will need to put in a rule for that too, otherwise a blank page will load.
If regardless of the custom domain visited (example.com, site.com, website.com, etc.) the destination is always the same (e.g. mywebsite.com/path) then you can have one rule to cover everything
/* https://detination.tld/path 301!
_redirects or netlify.toml
[[redirects]]
from = "/*"
to = "https://destination.tld/path"
status = 301
force = true
This would cover any custom domain as well and the <site>.netlify.app domain too.
Thanks for the thorough reply. I’ll probably leave it as is for the moment and update it next time I have some time and/or when I need to add more domains.
To clarify on a few points in case it helps others…
Each custom domain will have one destination. The destination could be shared between multiple domains (such as exampledomain.com and example-domain.com both pointing to the same destination), but generally it’s one unique destination per domain name. So a single broad rule won’t quite help out here.
Using the _redirects file and format without any sort of public folder specified (just leaving it blank to default to the project root) doesn’t seem to work for me. It’s possible there was an issue with my formatting, but even doing a single simple rule didn’t seem to have any effect until I put the more verbose rules in netlify.toml. I may try making and configuring the site to use an arbitrary public folder and sticking it in there to see if that has an effect.
I’m now leaning towards a build script to read a single file (probably tab separated values, one record per line) with nothing but entry domain and destination specified. The build script will generate the rules for each to cover with/without https and www. If I can’t get a _redirects file working as mentioned above, I’ll probably just commit the build results to the repo instead of making Netlify build it. That way I can be sure the toml file will be available for preprocessing.
Setting up a public directory and specifying that in the site’s build settings made the _redirects file work for me. It’s possible that I had just misconfigured something initially, but it really seems that a public directory needs to be set for _redirects to be read, whatever you wish to call it. Doing so has given me a much more legible file. It’s still four lines per domain name, but that’s pretty simple to copy and paste and less to deal with than 20 lines per domain name.
I may still create the build script to use just one line for each bare apex domain and its destination as a source file. Then I can create the four lines and extra syntax programmatically. But that’s a relatively low priority at this point.
I know I said “last reply”, but just sharing the repo in case anyone finds it useful. I went ahead and made the build script. As a result, each domain/destination now just looks like the following and the rest is built by Netlify as a _redirects file.