Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

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

At the moment I create a .htaccess file which redirects non-existent sub domains to the root URL. E.g

<VirtualHost *:80>
    DocumentRoot /var/www/example/public

    ErrorLog /var/log/apache2/
    LogLevel warn
    CustomLog /var/log/apache2/ combined

# Redirect for non existent subdomains
<VirtualHost *:80>
    ServerAlias *
    DocumentRoot /var/www/example/redirect


Redirect 301 /

Is there a way I can do this all in Apache? I want this to be search engine friendly by redirecting with the 301 code to tell them that the subdomains doesn't exist.

share|improve this question
up vote 0 down vote accepted

The Redirect Directive is described thus,

Description: Sends an external redirect asking the client to fetch a different URL

Syntax: Redirect [status] URL-path URL

Context: server config, virtual host, directory, .htaccess

Override: FileInfo

Status: Base Module: mod_alias

Bold emphasis mine. You should be able to put the redirect into the VirtualHost section, without the need for the .htaccess.

If you want to say the domain doesn't exist, you should return 410 (gone), although I'm not sure of your intent. 302 might be sufficient.

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.