I've just realised that the server is aware of the whole URL. The browser sends the path to the server in a http GET request, but the hostname is also sent in a 'Host:' header of the same request.
I know, I did trying using '*', but it wouldn't let me. I might ask support to do so, but it's not quite what I want anyway.
So this would allow for:
example.com/path -> example.com/path (or www.example.com/path)
www.example.com/path -> example.com/path (or www.example.com/path)
*.example.com/path -> example.com/ (or www.example.com/)
usedsub.example.com/path -> usedsub.example.com/path (or www.usedsub.example.com/path)
www.usedsub.example.com/path -> usedsub.example.com/path (or www.usedsub.example.com/path)
*.usedsub.example.com/path -> usedsub.example.com/ (or www.usedsub.example.com/)
Note that the first 3 are just special cases of the last 3.
For unused subdomains, the path and subdomains would simply be dropped and redirect to the homepage of the primary domain. This should really be default. It just seems so much more professional to have the unused subdomains of a site not be NXDOMAIN.
However, this is not consistent with serving 404 pages for unused URLs.
Ideally, I want to serve 404 pages that are aware of the full URL, not just the path. Then I can offer automatic suggestions or have more programmatic control over how redirects work.
How do I do this?
Yes, I've done so for some used subdomains.