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 have a website ( and I would like to redirect all my secondary domains (, and all the subdomains of these domains (*, * to the main domain, (because I want the latter to be the only URL to gain access to the site).

For this purpose, I have created an Apache Virtual Host to catch all these possibilities and redirect them (after having configured my DNS, that goes without saying). I put this configuration in a file named "999-catchall" in the folder "sites-enabled" of Apache. NB : I use this name to be sure that it will be the last vhost checked, because I have also my default vhost (000-default for and a vhost for my webmail (001-webmail for

Here is the content of this "999-catchall" file:

<VirtualHost *:80>
        # catch...
        ServerAlias * * *
        # ...and redirect
        RewriteEngine on
        RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
        RewriteRule ^(.*)$$1 [R=301,L]

This configuration works for,, *, and * but not for * Example : If I type, I'm redirected to, but if I type, I'm not (I just have a "Server not found" error).

Is my method correct? Do you see where my mistake is?

EDIT : My mistake, my DNS server wasn't configured properly for * So this configuration works, if it can help someone who would want to do the same thing.

share|improve this question

I'm no Apache expert, but have considered changing:

ServerAlias * * *

edit: On rereading the OP, it looks like you're already configuring in a different file, which I don't believe is allowed. That, however, may be part of the problem. If I'm following you correctly, the response is from that file, not the config posted here.

share|improve this answer
Yes, I'm configuring in another file (000-default). Why isn't it allowed? and what is the good pratice? I'm a bit lost... – Flackou Sep 17 '09 at 19:44

I would write RewriteRule like

RewriteRule ^(/.*)$$1 [R=301,L]

to make sure I'm not appending random junk to .com domain

and then enable RewriteLogLevel 3 and RewriteLog and examined generated file for other mistakes.

If you could attach output of wget on one of domains which doesn't work it would help.

share|improve this answer
wget result : Resolving failed:: No address associated with nodename. wget: unable to resolve host address `' – Flackou Sep 17 '09 at 20:07
... OK this result made me realize that it could be a DNS misconfiguration. Actually, it is the case. I hadn't specified a DNS entry for *! My "after having configured my DNS, that goes without saying" was a bit presomptuous! I will wait for the DNS to propagate before celebrating, but it seems that the error comes from this. Sorry for the disturbing. – Flackou Sep 17 '09 at 20:14

I don't know if you have any virtual hosts on the machine, but try checking to see that isn't already defined somewhere else in your config

share|improve this answer
That's what I thought too, but if it was the case (if was caught by another vhost), it should render something and not "Server not found", no? – Flackou Sep 17 '09 at 19:43

NB : I use this name to be sure that it will be the last vhost checked, because I have also my default vhost (000-default for and a vhost for my webmail (001-webmail for

Vhost config files are checked alphabetically starting at 0, and the first one will act as catchall.

From Apache (1.3 through 2.2) Documentation:

The first vhost on this list (the first vhost in the config file with the specified IP address) has the highest priority and catches any request to an unknown server name or a request without a Host: header field.

Your other option may be to use a _default_:80 virtualhost. See Using _default_ vhosts

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.