Question

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

###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!


Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Accepted Answer

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.

Have you checked in which virtualhost definition your rewrites reside? I you the virtualhosts separated between port 80 and port 443, it might go wrong

<virtualhost *:80=“”>

vs

<virtualhost *:443=“”>

if you try to rewrite from https://www to https:// (without www) in the virtualhost listening on port 80, it won’t work. Because it will never get there (it’s not https there). That rewrite rule should be in the virtualhost listening on port 443 - or make sure your virtualhost listens to any port, of course.

RewriteCond %{HTTP_HOST} ^www.yoursite.com [NC]
RewriteRule ^(.*)$ http://yoursite.com/$1 [L,R=301]

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel