Can you use virtual host config (.conf) to redirect WWW domain to Non-WWW?

July 18, 2014 19k views

I've recently changed the config files of my virtual hosts and rather than use an .htaccess file at the directory root I wanted to use the config file for server optimization. However it's not working.

After turning rewrite engine on as follows:

<IfModule mod_rewrite.c>
    Options +FollowSymlinks
  # Options +SymLinksIfOwnerMatch
    RewriteEngine On
  # RewriteBase /
</IfModule>

And using this rewrite condition...

<IfModule mod_rewrite.c>
    RewriteCond %{HTTPS} !=on
    RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
    RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
</IfModule>

With the virtual host set as follows...

<virtualhost>
    ServerName: example.com 
    ServerAlias: www.example.com 
    .......
</virtualhost>

The WWW URL is still NOT redirecting to non-WWW URL.

What am I missing? Is this not possible to do using Digital Ocean? I could really use someones help. I've tried other methods such as update the A and CNAME records based on some of the communities suggestions but still have not been able to get this to work. Help anyone? Please and thank you!

2 Answers

When configuring Virtual Hosts you can 1. upload an .htaccess file (which is not recommended by Apache as it can slow down server) or 2. edit your virtual hosts config file directly. I took the challenge of editing the Virtual Host Server Config file directly using HTML5 Boilerplate and Google Web Toolkit recommendations.

So how did I do it you might ask...

Editing your main server config file (e.g. /etc/apache2/apache2.conf) is fairly simple but when trying to do so per Virtual Host the structure is slightly different.

In your virtual host config file (e.g. /etc/apache2/sites-available/yoursite.com.conf) your setup should look as follows:

<virtualhost> 
    ServerAdmin admin@yoursite.com 
    ServerName yoursite.com 
    ServerAlias www.yoursite.com 
    DocumentRoot /var/www/yoursite.com/public_html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</virtualhost>

Now, suppose you wanted to make sure that all WWW URL's are redirected to non-WWW URL's you would have to do the following:

<virtualhost> 
    ServerAdmin admin@yoursite.com 
    ServerName yoursite.com 
    ServerAlias www.yoursite.com 
    DocumentRoot /var/www/yoursite.com/public_html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    # You must your the <directory> container. For more information see filesystem containers 
    # http://httpd.apache.org/docs/current/sections.html#virtualhost 

    <directory />

        # You more than likely can the remove the <ifModule> container as your Rewrite 
        # Engine is probably turned on in your main server config file 
        # (check here: /etc/apache2/apache2.conf) but it is here for structure. 

        <IfModule mod_rewrite.c>
                Options +FollowSymlinks
                RewriteEngine On
        </IfModule>

        # Redirects WWW URL's to Non-WWW URL's

        <IfModule mod_rewrite.c>
                RewriteCond %{HTTPS} !=on
                RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
                RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
        </IfModule>

         # If there is a specific directory within your Virtual Host that you wanted to 
         # modify, you would have to nest another <directory> container within the root directory
         # of virtual host previously declared with reference to the directory.  

         <directory /some-directory> 
                 # your rules would go here.
         </directory>

    </directory>
</virtualhost>

For cleaner (reusable) code it may be best to declare server global directives in the your main server config file (again you can find it here: /etc/apache2/apache2.conf). However be careful when doing so as this may not be right for every project. Here is a great beginners introduction to Apache Server Config's by Justin Ellingwood.

Side Note: I'm not an experienced Server Admin by any means and am learning while keeping best practices in mind. Should anything stated in response that is not accurate or not a good practice please comment. Hoping this might spark some general discussion among Server Ninjas & Novices on Server Best Practices for optimal performance on Digital Ocean.

  • it works! :) I have one 443 host and one 80 and ServerAlias are not working.

    One thing, I have used a different code for www redirect inside VirtualHost directives (not using ifModule):

    <Directory "/var/www/html/site1.com/public_html">
    RewriteEngine On
    RewriteBase /
    RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
    RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
    </Directory>
    

    For the 443 website, you might want to use to use <VirtualHost> directive inside conf.d/ssl.conf in httpd folder (centos). For me it worked instead of using <VirtualHost> inside sites-available (website on port 80 use sites-enabled, website on port 443 use virtualhost inside ssl.conf).

    Thank you!

RewriteCond %{HTTP_HOST} ^www.yoursite.com [NC]
RewriteRule ^(.*)$ http://yoursite.com/$1 [L,R=301]
Have another answer? Share your knowledge.