EdG
By:
EdG

Using CNAME for www redirection

April 27, 2015 23.3k views
DNS

Currently, www.domain.net does not resolve to my http://domain.net.

Can I use a DNS CNAME record to fix this?

Would I simply put the 'Enter Name' field as 'www.domain.net' and the IP address used in the A record for the 'Enter Hostname' field?

Thanks,

Ed

3 Answers

Assuming that you have the default created DNS record set up it will include a main record like this:

@  IN  A  [DROPLET_IP]

We can redirect a CNAME to this by doing the following:

www  CNAME   @

This will direct requests for www to the same IP as those for the domain without the www subdomain. You will still need to ensure that your web server is configured to handle the www subdomain properly. If you only run a single site on this droplet there shouldn't be any more to do. If you are running multiple virtualhosts on the droplet you will need to either Add a ServerAlias directive (in Apache) or add the www.domain.com name to the server_name variable (in NGINX).

I'm having this same problem and I've read multiple potential resolutions, none of which work for me. In this instance, if I try: www CNAME @, I get a message that only one @ record is allowed. I have tried adding www as a CNAME and, alternatively, as an A record but to no avail. Everything else works, such as, ww.domainname .com and wwww.domainname.com, for example. Any suggestions are appreciated. I do not see any issues with my virtual host configuration.

SOLVED (sortof). I noticed that any ServerAlias in my virtual host would end up with "index of /" and was not being redirected to the domain. I finally took out all aliases from the virtual host and everything works fine now. Obviously, I am missing a connection about why this is working and why it wasn't before. Can anyone explain why?

  • To expand on Ryan's answer above and specific to droplets maybe setup through the LAMP one-click, and that flavor of virtual host configuration...

    A CNAME record to @ or the naked domain name (i.e. mysite.com) will work fine. DNS records are just going to get the request to the right IP. After that, the apache setup (or whatever) listening on that IP needs to handle the request properly. When you typed in mysite.com, it worked fine because you probably had a site enabled called mysite.com. To get your server to respond to www.mysite.com, you will need to add a ServerAlias to the domain conf file. Something like this

    <VirtualHost *:80>
    ServerAdmin me@mysite.com
    ServerName mysite.com
    ServerAlias www.mysite.com
    DocumentRoot /var/www/mysite.com/public_html

        <Directory /var/www/mysite.com/public_html/>
            Options Indexes FollowSymLinks
            AllowOverride All
            Require all granted
        </Directory>
    
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
    
        <IfModule mod_dir.c>
            DirectoryIndex index.php index.pl index.cgi index.html index.xhtml index.htm
        </IfModule>
    

    </VirtualHost>

Have another answer? Share your knowledge.