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.

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

<VirtualHost *:80>
    ServerAdmin www@example.co.uk
    ServerName example.co.uk
    ServerAlias www.example.co.uk
    DocumentRoot /var/www/example/public

    ErrorLog /var/log/apache2/example.co.uk-error.log
    LogLevel warn
    CustomLog /var/log/apache2/example.co.uk-access.log combined
</VirtualHost>

# Redirect for non existent subdomains
<VirtualHost *:80>
    ServerAdmin www@example.co.uk
    ServerAlias *.example.co.uk
    DocumentRoot /var/www/example/redirect
</VirtualHost>

/var/www/example/redirect/.htaccess

Redirect 301 / http://www.example.co.uk

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

1 Answer 1

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

 
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.