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.

I have a website (domain.com) and I would like to redirect all my secondary domains (domain2.com, domain3.com) and all the subdomains of these domains (*.domain.com, *.domain2.com...) to the main domain, www.domain.com (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 www.domain.com) and a vhost for my webmail (001-webmail for webmail.domain.com).

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

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

This configuration works for domain.com, domain2.com, *.domain2.com, domain3.com and *.domain3.com but not for *.domain.com. Example : If I type blabla.domain2.com, I'm redirected to www.domain.com, but if I type blaba.domain.com, 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 *.domain.com. So this configuration works, if it can help someone who would want to do the same thing.

share|improve this question

4 Answers 4

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

ServerName www.domain.com
ServerAlias domain.com domain2.com domain3.com *.domain.com *.domain2.com *.domain3.com

edit: On rereading the OP, it looks like you're already configuring www.domain.com 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 www.domain.com response is from that file, not the config posted here.

share|improve this answer
    
Yes, I'm configuring www.domain.com 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 ^(/.*)$ http://www.domain.com$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 blabla.domain.com... failed:: No address associated with nodename. wget: unable to resolve host address `blabla.domain.com' –  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 *.domain.com! 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 blaba.domain.com 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 blabla.domain.com 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 www.domain.com) and a vhost for my webmail (001-webmail for webmail.domain.com).

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

 
discard

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