What I'd like apache to do is match every request for example.com and redirect to www.example.com. Any other hosts sent by a browser that are not on this domain should return a 404.
Here are my two vhosts:
example.com:
<VirtualHost *>
ServerAdmin [email protected]
ServerName www.example.com
ServerAlias *.example.com
DocumentRoot /var/www/example.com/
<Directory />
Options FollowSymLinks
AllowOverride All
</Directory>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^.*example\.com$ [NC]
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com$1 [R=301,L]
ErrorLog /var/log/apache2/error.log
LogLevel warn
CustomLog /var/log/apache2/access.log combined
</VirtualHost>
000-default:
<VirtualHost *>
Redirect 404 /
</VirtualHost>
What actually happens is that "www.example.com" is beeing served as it should, a host header of "test" is beeing 404'd as it should, while "example.com" is beeing 404'd, and it shouldn't.
It's like apache matches "www" to the ServerName directive, but the ServerAlias is beeing ignored, and a subdomain of "sub" matches the default vhost instead.