Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I can't seem to explain Apache to accept requests on the naked domain but to ignore anything coming from a subdomain. I have the feeling this should be easy but everything I find is related to redirecting subdomain requests, not ignoring them.

My current setup:

<VirtualHost *:80>
  ServerName example.com
  DocumentRoot /var/www/example
</VirtualHost>

Accessing something.example.com directs to example.com, I would like it to fail with a 404 not found. I can probably exclude everything with mode_rewrite but is that the best solution? Or should I do this on the DNS level?

Thank you for helping me out with such a n00b question.

share|improve this question

3 Answers 3

You can create a second virtual host to act as a catch-all and just return 404s. More or less like this:

NameVirtualHost *:80

<VirtualHost *:80>
  ServerName example.com
  DocumentRoot /var/www/example
</VirtualHost>

<VirtualHost *:80>
  ServerName  a.example.com
  ServerAlias *.example.com
  RewriteRule ^ - [R=404]
</VirtualHost>
  • If a request arrives on *:80 and asks for example.com it gets handled by the first virtual host.
  • If a request arrives on *:80 and asks for *.example.com it gets handled by the second virtual host.
share|improve this answer
    
Thanks but... using this and accessing something.example.com still ends up in the 1st virtual host, not the second. Is there a possibility Apache thinks the naked domain is the wildcard one? –  Cimm Jan 5 at 13:03
    
You do have the NameVirtualHost directive, right? –  mauro.stettler Jan 5 at 13:14
    
Yes and no. I did not add it as it's already specified in the ports.conf file (installed Apache 2 via the Debian package). That should be fine, no? –  Cimm Jan 5 at 13:40

Based on mauro.stettler's answer I found something that works:

<VirtualHost *:80>
   ServerName example.com
   DocumentRoot /var/www/example
</VirtualHost>

<VirtualHost *:80>
   ServerName *.example.com
   ServerAlias *.example.com
   Options -Indexes
</VirtualHost>

It throws a 403 Forbidden instead of a 404 Not found but that will do.

Without the -Indexes part it shows the directory structure of my /var/www directory. Would that be the default Apache uses as a fallback?

share|improve this answer
1  
ServerName doesn't take wildcards so what's actually happening is the ServerAlias is matching everything bar example.com. You might try taking the ServerName *.example.com out of the second vhost. If that fails use something like a.example.com with a wildcard ServerAlias. –  Iain Jan 5 at 19:33

First virutal host is a default one and will match every request for unknow VH

Do something like this:

# First VH
<VirtualHost *:80>
   ServerName loalhost
   DocumentRoot /var/www/default
</VirtualHost>

# Second and more VH
<VirtualHost *:80>
  ServerName example.com
  DocumentRoot /var/www/example
</VirtualHost>
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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