1

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    192.168.0.1
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:

/etc/apache2/sites-available/default
/etc/apache2/sites-available/mydomain
/etc/apache2/sites-available/myOtherDomain
/etc/apache2/sites-available/yetAnother

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

<VirtualHost *:80>
    ServerAdmin [email protected]
    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.

2
  • 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, 2013 at 19:08
  • They resolve just fine... there are no wildcard A records or any other subdomain records.
    – inorganik
    Apr 3, 2013 at 19:31

2 Answers 2

2

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.

3
  • But the CNAME record is supposed to send www.mydomain.com to mydomain.com. It's an http level redirect
    – inorganik
    Apr 3, 2013 at 18:28
  • That's not how standard dns works.. it might be your provider doing it.
    – Mike
    Apr 3, 2013 at 19:04
  • 2
    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, 2013 at 21:35
0

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

<VirtualHost *:80>
    ServerAdmin [email protected]
    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.

4
  • 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, 2013 at 19:37
  • @etagenklo host www.mydomain.com: www.mydomain is an alias for mydomain.com. mydomain.com has address 192.68.0.1 mydomain.com mail is handled by 10 mail.mydomain.com.
    – inorganik
    Apr 3, 2013 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, 2013 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, 2013 at 18:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .