How to redirect www to non-www within domains?

March 18, 2015 6.1k views

What setting to I have to put in DNS at DO for a domain, so that to be redirected to

And what setting to put so that if someone tries to access a non existing subdomain (like to be redirected to Or if not in DNS settings, where?


1 comment
  • Lets start with www,

    you will want to add an A-record with just www in it for your domain.

    this will make www go to your droplet.

    Apache Config File
    after that, you will want to add www as an Alias in your site config file, (if you are using Apache). This should be located in /etc/apache2/sites-enabled

    find the section where it says:


    and add this under it:


    While you are there, make sure that you have a section that has this(replace yourwebfolder with whatever you use for your root web folder)

        <Directory /var/www/yourwebfolder>
            Options Indexes FollowSymLinks MultiViews
            AllowOverride all
            Order allow,deny
            allow from all
                Require all granted

    reload Apache and now you should be able to get to your site using either or just

    .htaccess file
    lastly, add this to your .htaccess file in your web folder:

    #redirect www to website root
    RewriteEngine On
    RewriteBase /
    RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
    RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

    that will at least take care of www. You can use the same idea for other other sub-domains.

2 Answers

I used an A record and that solved the issue. Thanks!

DNS doesn't do redirection. You have to configure it in your webserver.

Have another answer? Share your knowledge.