albasiero
By:
albasiero

How to redirect www to non-www within domains?

March 18, 2015 6.6k views
DNS

What setting to I have to put in DNS at DO for a domain, so that www.domain.com/whatever to be redirected to domain.com/whatever?

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

Thanks.

1 comment
  • Lets start with www,

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

    ServerName yourdomain.com
    

    and add this under it:

    ServerAlias www.yourdomain.com
    

    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
        </Directory>
    
    

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

    .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.

3 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.

@sierracircle What Apache version is the advice for? What Linux distro is this for? I tried this and got this error on a CentOS 7 running Server version: Apache/2.4.6 (CentOS) and I got this error:

Job for httpd.service failed because the control process exited with error code. See "systemctl status httpd.service" and "journalctl -xe" for details.

First of all, aren't we supposed to be editing our Apache Config Files located in /etc/httpd/sites-available? not sites-enabled?
Secondly, is our example.com.conf supposed to look like this advice:


<VirtualHost *:80>

    ServerName www.example.com 
    ServerAlias example.com 
    DocumentRoot /var/www/example.com /public_html
    ErrorLog /var/www/example.com /error.log
    CustomLog /var/www/example.com /requests.log combined
</VirtualHost>
    <Directory /var/www/yourwebfolder>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride all
        Order allow,deny
        allow from all
            Require all granted
    </Directory>

This is my question. And lastly, isn't yourwebfolder supposed to be called a document root? Or are they different? My last question is for Digital Ocean. Can we please get a qualified document that helps CentOS 7 customers make it so the same page with both www.example.com and example.com have good DNS but we want people who go to both pages only END UP on the example.com domain please? Thanks in advance.

  • If you run sudo apachectl configtest, it will let you know if there are any errors in your config files. You can also view the logs of the Apache's startup by running sudo journalctl -xe as mentioned in the error.

    The instructions that @sierracircle posted are correct. So, run the commands above and let's find out what's wrong. Make sure you replace /var/www/yourwebfolder with your actual document root path (/var/www/example.com/public_html) because you want the <Directory> block to match files and directories under that path.

    One thing I noticed is that you have spaces in some of your paths. I'm not sure if that's how your config actually is, or if they were simply pasted wrong here.

        DocumentRoot /var/www/example.com /public_html
        ErrorLog /var/www/example.com /error.log
        CustomLog /var/www/example.com /requests.log combined
    

    should be

        DocumentRoot /var/www/example.com/public_html
        ErrorLog /var/www/example.com/error.log
        CustomLog /var/www/example.com/requests.log combined
    

    As for sites-enabled and sites-available—ideally, all the files that are in sites-enabled would actually be symbolic links to the actual files in sites-available, so you can enable and disable config files without completely deleting them. If you run ls -lah /etc/apache2/sites-enabled, you will be able to see if that's the case. In the example below, file.conf is indeed a symlink.

    $ ls -lah
    total 8
    drwxr-xr-x  3 user  user   102B Jun  1 17:53 .
    drwxr-xr-x  4 user  user   136B Jun  1 17:52 ..
    lrwxr-xr-x  1 user  user    28B Jun  1 17:53 file.conf -> ../sites-available/file.conf
    

    Finally, if you don't have SSL set up, make sure you replace https with http in the .htaccess config:

    RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
    
    • Everything that @kamaln7 said.

      You probably are already aware of this, but just to mention it:

      where it says example.com you of course will put your domain instead.

      Also, you may need to enable rewrite:

      sudo a2enmod rewrite
      
      • Thanks that is correct. I do replace my https://www.example.com with my own Authoritatively Signed SSL cert.

        And I have verified that mod_rewrite module is running in my Apache. Running httpd -M gives me :

        ...
        remoteip_module (shared)
         reqtimeout_module (shared)
         rewrite_module (shared)
         setenvif_module (shared)
         slotmem_plain_module (shared)
        ...
        

        Thank you for the help sir.

      • Thank you I believe the spaces are a copy paste thing. I have verified that I have no spaces. I think the issue is that I was expecting to put the protocol in front and that an https://www.example.com would go to a https://example.com. It DOES. The only thing is that you cant put the http:// or the https:// in front on Chrome. At least for me at this time. If I put in www.example.com it takes me to https://example.com which is essentially what I wanted. But if I put in https://www.example.com it stays in https://www.example.com. Is this how it is supposed to be. I'm sorry this is my first Authoritative SSL Cert.
        The way I have it now is as follows:

        My /etc/httpd/sites-available/example.com.conf looks like this now thanks to this community:

        /etc/httpd/sites-available/example.com.conf:

        VirtualHost *:80>
        
        ServerName example.com
        ServerAlias www.example.com
        Redirect permanent / https://example.com
        DocumentRoot /var/www/example.com/public_html
        ErrorLog /var/www/example.com/error.log
        CustomLog /var/www/example.com/requests.log combined
        </VirtualHost>
        

        And my /var/www/example.com/public_html/.htaccess file:

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

        Thanks for the help here guys.

Have another answer? Share your knowledge.