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

I setup a wildcard A record on my domain registrar. Now if a user access a missing subdomain on my domain, they will be redirected to the homepage. Currently my initial setup was this:

<VirtualHost *:80>
  ServerAdmin webmaster@localhost
  RewriteEngine On
  RewriteRule ^(.*)$$1 [R]
  DocumentRoot /var/www
  <Directory />
    Options FollowSymLinks
    AllowOverride None
  # more below...

Any wildcard subdomain or if my IP is entered via URL will redirect to the homepage. Can I do something about this that will redirect (HTTP redirect perhaps) the wildcard subdomains to 404 page instead of to homepage?

share|improve this question

Disable the RewriteEngine, and specify an empty directory as the DocumentRoot:

<VirtualHost *:80>
  ServerAdmin webmaster@localhost
  DocumentRoot /var/www/empty-directory-that-i-just-created
  # more below...

Since this VirtualHost uses an empty directory as DocumentRoot, Apache won't be able to fulfill any requests; it'll return 404s for all of them.

share|improve this answer

You could also consider the option of rewriting to a statuscode 403 with code [F] and having a 403 errorpage if 'a valid permanent errorcode' is the reason you want a 404 code and not exactly this code.

DocumentRoot /some/valid/directory
ErrorDocument 403 /nothere.html

RewriteRule /nothere.html /nothere.html [L]
RewriteRule . - [F,L]

The rewriterule for the 403 page is to avoid a configuration error.

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.