Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

My site works both with and without the www subdomain (i.e., both and work). However, also works and the same website content is served.

How can I disable all subdomains except I want all other subdomains to return a 404 error or just not show anything.

The config now is as follows:

NameVirtualHost *:80

<VirtualHost *:80>

share|improve this question

Add to VirtualHost config and enabled mod_rewrite:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www\.|)domain\.com$ [NC]
RewriteRule .* - [F]
share|improve this answer



and comment out #ServerAlias...

then when you request you will get to default apache site apache2ctl -S will show you which one is default...

or if you want to do this via mod_rewrite.

RewriteCond %{HTTP_HOST} !^www\.domain\.com$ 
RewriteRule (.*) - [F]

This will give 403 for any request that's not designated for

Or if you want you can redirect them all to www so they don't use the other subdomain options.

 RewriteCond %{HTTP_HOST} !^www\.domain\.com$  
 RewriteRule (.*) [R=301,L]
share|improve this answer
You forgot to say the most important thing: the default server for apache is the first file on apache/sites-enabled. Very smart move from Apache! I created a a-catch-all config file there and problem solved. – JohnPristine Apr 29 '14 at 6:34

You can create a catch all vhost. Make sure it is the last vhost you define as anything not previously matched will be caught by it.

<VirtualHost *:80>
        ServerName      catchall
        ServerAlias *
        DocumentRoot /var/www/catchall

To show nothing have a simple blank index.html file in the DocumentRoot of the vhost.

share|improve this answer
Thanks! Can I send 404 from catchall using apache config only? – Andrey Feb 19 '11 at 12:20
Strictly speaking it's not a 404 as the resource has been found. – Iain Feb 19 '11 at 12:50
If you want to send a 404, DON'T create a catchall host and don't let your virtualhost match domains except the ones you want results for. Then apache will send the right errors about resources not being found. Don't try to make apache match something and then pretend it didn't! – Caleb Feb 19 '11 at 13:27

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.