Take the 2-minute tour ×
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 mydomain.com. I'd like to redirect www.mydomain.com to the naked domain, mydomain.com. So I set up the following DNS. (The IP is just for show)

A        mydomain.com
CNAME    www             mydomain.com

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>
    ServerAdmin sample@email.com
    ServerName mydomain.com
    ServerAlias mydomain.com

What's wrong with this setup that's it's not redirecting www.mydomain.com to mydomain.com? 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 (*.mydomain.com)? –  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 ^(.*) http://mydomain.com/$1 [L,R=301]

Edited for regex syntax.

share|improve this answer
But the CNAME record is supposed to send www.mydomain.com to mydomain.com. 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>
    ServerAdmin sample@email.com
    ServerName mydomain.com
    ServerAlias mydomain.com www.mydomain.com

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 www.mydomain.com ? –  etagenklo Apr 3 '13 at 19:37
@etagenklo host www.mydomain.com: www.mydomain is an alias for mydomain.com. mydomain.com has address mydomain.com mail is handled by 10 mail.mydomain.com. –  inorganik Apr 3 '13 at 19:41
I'm a bit stuck currently. If www.mydomain.com is resolved to mydomain.com via CNAME (what is done according to your host command), this should be exactly the same as opening mydomain.com directly, so please excuse my stupid questions: Did you issue the host www.mydomain.com 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 mydomain.com` on the client definitely opening mydomain.com 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 www.mydomain.com 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.