How to Properly Redirect WWW to Non-WWW URLs in Apache VirtualHost Configuration


3 views

While your current setup works, there are several inefficiencies:

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

This .htaccess approach requires file system access for each request and doesn't leverage Apache's more efficient VirtualHost directives. Additionally, having both domains point to the same DocumentRoot means unnecessary filesystem checks.

Here's the proper way to handle this in your VirtualHost configuration:


    ServerName www.example.com
    ServerAlias example.com
    Redirect permanent / http://example.com/



    ServerName example.com
    DocumentRoot /var/www/site
    
        Options FollowSymLinks
        AllowOverride None
        Require all granted
    

The key advantages of this approach:

  1. Eliminates need for .htaccess processing
  2. Reduces filesystem operations
  3. Proper HTTP status codes (301 permanent redirect)
  4. Clear separation of redirect and content serving

For modern HTTPS sites, you'll want this configuration:


    ServerName www.example.com
    SSLEngine on
    # SSL certificates here
    Redirect permanent / https://example.com/



    ServerName example.com
    DocumentRoot /var/www/site
    SSLEngine on
    # SSL certificates here
    
        Options FollowSymLinks
        AllowOverride None
        Require all granted
    

After making changes:

sudo apachectl configtest
sudo systemctl reload apache2

Verify with curl:

curl -I http://www.example.com
curl -I https://www.example.com

You should see "301 Moved Permanently" for www URLs and "200 OK" for non-www.


While using .htaccess for redirects works, it's not the most efficient solution. Apache processes .htaccess files for every request, creating unnecessary overhead. The better approach is to handle redirects at the server configuration level.

Here's how to implement the redirect directly in your VirtualHost configuration:

<VirtualHost *:80>
    ServerName www.example.com
    Redirect 301 / http://example.com/
</VirtualHost>

<VirtualHost *:80>
    ServerName example.com
    DocumentRoot /var/www/site
    
    <Directory "/var/www/site">
        Options Indexes FollowSymLinks
        AllowOverride None
        Require all granted
    </Directory>
</VirtualHost>

This configuration provides several advantages:

  • Eliminates the need for .htaccess processing
  • Handles redirects at the server level for better performance
  • Uses proper 301 redirects for SEO benefits

If you need more complex redirect rules, you can use mod_rewrite within the VirtualHost:

<VirtualHost *:80>
    ServerName www.example.com
    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
    RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]
</VirtualHost>

For HTTPS sites, remember to create separate configurations for port 443:

<VirtualHost *:443>
    ServerName www.example.com
    SSLEngine on
    # SSL certificate configuration...
    Redirect 301 / https://example.com/
</VirtualHost>

Always test your changes:

sudo apachectl configtest
sudo systemctl restart apache2

Use curl to verify the redirect:

curl -I http://www.example.com