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

How can I send 404 if subdomain is not found? For example if I have something like this:

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

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

and open example.com, it opens correct page, if I try with subdomain www.example.com, it opens correctly. However if I try test.example.com (or any other subdomain) it still opens the same page, but I would like to send 404. Looks like very similar question to this one: apache2-virtualhosts-and-wildcard-subdomains, but I don't find a way to create 404 response.

share|improve this question

1 Answer 1

up vote 0 down vote accepted

I found a way to do that with RewriteRule like this:

RewriteEngine on
RewriteCond %{HTTP_HOST} !^(www\.)?example\.com$ [NC]
RewriteRule ^ - [R=404,L]

Don't know if it is the best way to do it, but it works for me.

share|improve this answer

Your Answer


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.