Sign up ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I have a domain, let's say I'd like to redirect to the naked domain, So I set up the following DNS. (The IP is just for show)

CNAME    www   

What happens though, is that Apache serves my default site (which is empty), rather than the mydomain site. Since I'm hosting multiple domains, I have:


The start of /etc/apache2/sites-available/mydomain looks like this:

<VirtualHost *:80>

What's wrong with this setup that's it's not redirecting to Thanks for your help.

share|improve this question
Did you check if the CNAME-record is already working and resolving (using commands like host or nslookup)? Did you also make sure that there's no other DNS A record for the www-subdomain or perhaps a wildcard A-record (* – etagenklo Apr 3 '13 at 19:08
They resolve just fine... there are no wildcard A records or any other subdomain records. – inorganik Apr 3 '13 at 19:31

2 Answers 2

up vote 2 down vote accepted

Apache itself won't redirect for you.. You'd have to use something like mod_rewrite to do that

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.mydomain\.com [NC]
RewriteRule ^(.*)$1 [L,R=301]

Edited for regex syntax.

share|improve this answer
But the CNAME record is supposed to send to It's an http level redirect – inorganik Apr 3 '13 at 18:28
That's not how standard dns works.. it might be your provider doing it. – Mike Apr 3 '13 at 19:04
The CNAME record only redirects DNS lookups to another DNS record, it doesn't affect anything else. DNS will only tell the browser which IP hosts the site, the browser then makes to the request to server asking for the site you put in the address bar (in this case with www.) You will still need to configure Apache correctly so it knows what pages to display to the use or to redirect them as above – Epaphus Apr 3 '13 at 21:35

Did a little reading up on Virtual Hosts and found I could do this:

<VirtualHost *:80>

which makes it work. Only problem is that the www remains in the address bar, rather than a redirect.

share|improve this answer
If this is working and the www remains in the address bar, this is a sign that your CNAME record is not working but there is an A-record for www, otherwise the subdomain could not be resolved. Again, what's the output of host ? – etagenklo Apr 3 '13 at 19:37
@etagenklo host www.mydomain is an alias for has address mail is handled by 10 – inorganik Apr 3 '13 at 19:41
I'm a bit stuck currently. If is resolved to via CNAME (what is done according to your host command), this should be exactly the same as opening directly, so please excuse my stupid questions: Did you issue the host command on the same machine youre trying to open the site from (Explanation: If not, maybe they're using different DNS resolvers and the one on the client machine doesn't know the CNAME yet)? Is opening` on the client definitely opening and not also the default vhost? – etagenklo Apr 3 '13 at 19:56
@etagenklo I get exactly the same result if I do it from a different machine. It was resolving to the default site until I added to the ServerAlias directive. – inorganik Apr 4 '13 at 18:43

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.