So I have a virtual server set up for www.company.com:

<VirtualHost *:80>
    ServerName www.company.com
</VirtualHost>

And then I would like to direct *.company.com to another site. How do I do it? I could think of www. and inside. being directed at specific vhosts but "the rest" directed to a generic vhost.

Apache version is 2.2.4

link|improve this question

43% accept rate
Although this may be useful locally, it is not best practice with regards to SEO and duplicate content wolf-howl.com/seo/wildcard-subdomains – Andy Sep 4 '09 at 10:33
It depends, obviously. I always set a "catchall" domain inserting the wildcard domain in the ServerAlias directive (yes, it is a quite different and more legal case ). The reason is I don't want that my customers could think I have something misconfigured on my servers, even if they have mistyped the url inserting a wrong subdomain name. – AlberT Sep 4 '09 at 10:39
feedback

1 Answer

up vote 8 down vote accepted

The solution at your answer:

<VirtualHost *:80>
    ServerName www.company.com
    ServerAlias company.com
    DocumentRoot /path1
</VirtualHost>
<VirtualHost *:80>
    ServerName *.company.com
    DocumentRoot /path2
</VirtualHost>

The polite use of wildcards "catch all" domains:

<VirtualHost *:80>
    ServerName subdomain1.company.com
    DocumentRoot /path/to/subdomain1
</VirtualHost>

<VirtualHost *:80>
    ServerName subdomain2.company.com
    DocumentRoot /path/to/subdomain2
</VirtualHost>

<VirtualHost *:80>
    ServerName  company.com
    ServerAlias *.company.com
    DocumentRoot /path/to/primary/domain+catchall
</VirtualHost>

Note that the order is meaningful, the catchall domain has to be the last one. This is particularly useful to avoid a mistyping in the client url raises an "inexistent host" error, letting the customer of your company think you are a bad server admin (not him a bad typer :P).

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.