How do I specify a parent domain in a multi-site install? I'm not sure the best title for this question so sorry if it was confusing.

Setup

I have a multi-site install that I regularly add new sites to.

They are generally in the format:


example.com (default)
sub1.example.com
sub2.example.com
sub3.example.com
sub3.com


I also have a wildcard dns and vshost so I don't have to edit them every time I create a new site:

example.com my.ip.add.ress
*.example.com my.ip.add.ress
sub3.com my.ip.add.ress

&lt;virtualhost *:80&gt;
  Name example.com
  alias *.example.com
  alias sub3.com
&lt;/virtualhost&gt;


Sites.php has a few entries like:

sites[sub3.com] = 'sub3.example.com'


All of this works great.

The problem.

Most of these sites are not publicly visible. being disabled with robots.txt and permission preventing users from see them. So I could care less about those. However some sites are public. In the example above the example.com and sub3.com are both public facing sites.

When a user visits rando.example.com the multi-site will send them to example.com but the url will appear as whatever they typed in. Also the sub3.com site will be visible as both sub3.com and sub3.example.com.

The question.

How do I, preferably using only Drupal, redirect the non-existent subdomains to the default. Not just show the default but actually do a 301 redirect to the url desired.

My attempts.

To my knowledge I can not solve this with:
-  redirect module as it does not let me specify a subdomain or a wildcard.
-  global redirect does not affect subdomains.
-  sites.php as it does not redirect but a sites[*.example.com] =&gt; example.com would be cool.

Qualifiers


I understand that I could fix this in the vhost by removing the wildcard name and just adding a new entry for each sub-domain. I do not want to have to edit my vhost every time their is a new sub-domain.
I understand I may be able to solve this with .htaccess files but I do not want to have to edit that file every time I create a new site either. And I don't really want it to be overridden from default. Plus I have my server pull the file only on reboot and I don't want to reboot apache just to add a site.
I understand that canonical urls would prevent bots from indexing unused sub-domains but that's not good enough I don't want users using a subdomain that doesn't exist.


My ideal solution would be a redirect that supports sub-domains or something similar. Changes to sites.php or database changes are the goal.
It is called multi-site aliasing, hence the results. Drupal offers you to server multiple domains from the same folder in order to preserve the paths in the database. So you would use the same folder for example.com and sub3.com because you might want to disable one of them later on. Aliasing is meant on a file level here. What you need is a redirect.

A 301 redirect code can be issued by your webserver-software itself (e.g. Apache/nginx config or apache .htaccess) or by sending a header via the scripting software.

The "drupal-way" solution for you would of course be to use a module for sending that header. My recommendation would be multisite_redirect, because it would only need a one time setup for your case outlined above.