How To Redirect From Non-WWW To WWW In Apache?

Published August 4, 2024

Problem: Redirecting Non-WWW to WWW in Apache

Websites can be accessed with or without the "www" prefix in their URLs. This can create duplicate content issues and affect search engine optimization. Redirecting from non-www to www addresses helps keep a consistent website identity and improves SEO performance.

Apache Configuration for WWW Redirection

Using the Redirect Directive

The Redirect directive in Apache is a simple way to redirect non-www to www URLs. To use it, add this line to your Apache configuration file:

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

This tells Apache to redirect all requests to the www version of your domain. The 'permanent' keyword means a 301 (permanent) redirect, which is good for SEO.

Tip: Test Your Redirect

After setting up the redirect, use a tool like cURL to test it. Run this command in your terminal:

curl -I http://example.com

Look for a 301 status code and the new location in the response headers.

Configuring Virtual Hosts for Redirection

Setting up separate virtual hosts for www and non-www versions of your website gives you more control over redirection. Here's how to do it:

  1. Create a virtual host for the non-www version:
<VirtualHost *:80>
    ServerName example.com
    Redirect permanent / http://www.example.com/
</VirtualHost>
  1. Set up a virtual host for the www version:
<VirtualHost *:80>
    ServerName www.example.com
    # Your main server configuration goes here
</VirtualHost>

This setup keeps the URL structure during redirection. For example, if someone visits 'http://example.com/page.html';, they will go to 'http://www.example.com/page.html';, keeping the original path and query parameters.

Implementing the Apache Redirect Solution

Steps to Configure Apache

To set up the Apache redirect from non-www to www:

  1. Open your Apache configuration file. Common locations:

    • /etc/apache2/apache2.conf (Ubuntu/Debian)
    • /etc/httpd/conf/httpd.conf (CentOS/RHEL)
  2. Add this code to your configuration file:

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

<VirtualHost *:80>
    ServerName www.example.com
    # Your main server configuration goes here
</VirtualHost>

Replace "example.com" with your domain name.

  1. Save the changes.

  2. Restart Apache to apply the changes:

    • sudo systemctl restart apache2 (Ubuntu/Debian)
    • sudo systemctl restart httpd (CentOS/RHEL)

Tip: Use ServerAlias for Subdomains

If you have multiple subdomains, use ServerAlias to include them in the redirect:

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

This will redirect all subdomains to the www version of your site.

Testing the Redirection

After setting up the redirect, test it:

  1. Open a web browser and enter your non-www URL (e.g., http://example.com).

  2. Check if it redirects to the www version (http://www.example.com).

  3. Test different pages and subfolders to make sure the redirect works for all URLs.

  4. Use curl to check the HTTP response:

curl -I http://example.com

Look for a 301 status code and the new location in the response headers.

If the redirect doesn't work, check these issues:

  • Make sure you restarted Apache after making changes.
  • Check the Apache error logs for configuration mistakes.
  • Verify that mod_alias is enabled.
  • If using .htaccess, make sure Apache allows .htaccess overrides.

By following these steps and testing, you can set up a working non-www to www redirect in Apache.

Alternative Methods for WWW Redirection

Using mod_rewrite for Redirection

The mod_rewrite module in Apache offers a way to handle URL redirection. To use it for non-www to www redirection, add these lines to your Apache configuration:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

This configuration turns on the rewrite engine, checks if the requested host doesn't start with "www.", and then redirects to the www version if it doesn't.

Pros of using mod_rewrite:

  • Flexible for complex redirection rules
  • Can handle various URL patterns and conditions

Cons of using mod_rewrite:

  • More complex syntax compared to the Redirect directive
  • Can be slower for simple redirections
  • Requires the mod_rewrite module to be enabled

Tip: Testing mod_rewrite Rules

Before applying mod_rewrite rules to your live server, test them using Apache's RewriteLog directive. Enable it in your configuration:

RewriteLog "/path/to/rewrite.log"
RewriteLogLevel 3

This will log all rewrite operations, helping you debug and refine your rules.

.htaccess File Approach

For shared hosting environments or when you can't access the main Apache configuration, you can use a .htaccess file for redirection. Create or edit the .htaccess file in your website's root directory and add:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

This achieves the same result as the mod_rewrite method in the main Apache configuration.

Considerations for shared hosting:

  • Make sure .htaccess files are allowed on your hosting plan
  • The .htaccess file is read on every request, which can impact performance
  • Changes in .htaccess take effect immediately without restarting the server
  • Be careful with syntax errors, as they can make your site inaccessible

When using .htaccess, test your changes carefully to avoid disrupting your website's functionality.