Redirect all unused subdomains


#1

So I want things like:
sub1.example.com -> example.com/sub1
sub1.example.com/path -> example.com/sub1/path
sub2.sub1.example.com -> example.com/sub1/sub2
sub2.sub1.example.com/path -> example.com/sub1/sub2/path
usedsub.example.com -> usedsub.example.com

I’ve added this DNS record:
NAME: *.example.com.
TYPE: CNAME
VALUE: example.com.

This is all working and requests get to the server for all subdomains.

The next step is how to make the server handle these redirects? What’s the best way? Is this any use?:

In the mean time, the ‘bad_httpd_conf’ error message is displayed for unused subdomains, which I much prefer to undefined behaviour based on the NXDOMAIN handling of the visitor’s ISP / DNS resolution service.

Really, I think this should either be default for unused subdomains, or there should be an easy web panel setup for it.

Thanks,
James.


#2

We can’t use wildcard domains unless we ask Support to enable them :wink:

You can select which directory a [sub]domain is served from in Manage Domains.

Unless you have lots of redirections it might be better to just use that.


#3

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.

Thanks,
James.