Redirecting requests from a non-www domain to a www domain maintains a single canonical domain and prevents duplicate content issues. Consistency in domain usage also helps improve search engine visibility, as it avoids splitting ranking signals between multiple hostnames.

The mod_rewrite module in Apache provides a flexible mechanism to manage these redirections, allowing traffic to be routed based on matching patterns in incoming URLs. This approach reduces confusion for users, preserves link equity, and ensures uniform resource locations across different entry points to the website.

Uniform redirection enhances overall user experience by ensuring that both non-www and www references resolve to a single destination. Implementing this strategy helps maintain a clear URL structure, which can be crucial for analytics, cache control, and optimizing site performance.

Redirect naked domain to www in Apache using htaccess

The .htaccess method is often chosen for its simplicity and ease of use. This method is ideal if you prefer not to modify the main Apache configuration files. .htaccess files are located in the root directory of your website, allowing you to manage redirection on a per-directory basis. This is particularly useful in shared hosting environments where you might not have access to the main server configuration.

However, using .htaccess can lead to performance overhead since Apache processes these files on every request. This method is best suited for smaller websites or those that require frequent changes to redirection rules without needing to restart the server.

  1. Enable rewrite module for Apache.
    $ sudo a2enmod rewrite # Ubuntu, Debian and SUSE variants
    Enabling module rewrite.
    To activate the new configuration, you need to run:
      systemctl restart apache2
    • Distribution with a2enmod support can simply run the command above without having to manually enable the required modules.
    • CentOS and Red Hat enables the module by default so requires no manual action to enable the modules.
    Options Debian, Ubuntu openSUSE and SLES Fedora Core, CentOS, RHEL macOS homebrew xampp
    a2enmod support yes yes no no no no
    Modules to install none
    Module name n/a rewrite
    Loadmodule directive n/a LoadModule rewrite_module <module_locations>/mod_rewrite.so
  2. Open or create a .htaccess file on the web folder where you want to set the redirection from.
    $ sudo vi /var/www/html/.htaccess
  3. Add redirect directive from within the .htaccess file.
    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^!simplified.guide$ [NC]
    RewriteRule ^(.*)$ http://www.simplified.guide/$1 [R=301,L]

    301 is equivalent to permanent redirect and you can use 302 instead for temporary redirect.

  4. Save and close the file.
  5. Hard-reload the web page (or clear browser's cache and reload) to test the redirect.

Redirect naked domain to www in Apache using Virtualhost

Using the VirtualHost method for redirection is more efficient in terms of server performance. This method is preferable if you have control over your server’s configuration and can modify the main Apache configuration files. VirtualHost configurations are processed once when Apache starts, making them more efficient for larger websites or those with higher traffic.

The VirtualHost method offers more centralized control over your server’s behavior, as it consolidates all configuration in one place. However, any changes require a server restart, which might be less convenient if you need to make frequent adjustments.

  1. Enable rewrite module for Apache.
    $ sudo a2enmod rewrite # Ubuntu, Debian and SUSE variants
    Enabling module rewrite.
    To activate the new configuration, you need to run:
      systemctl restart apache2
    • Distribution with a2enmod support can simply run the command above without having to manually enable the required modules.
    • CentOS and Red Hat enables the module by default so requires no manual action to enable the modules.
    Options Debian, Ubuntu openSUSE and SLES Fedora Core, CentOS, RHEL macOS homebrew xampp
    a2enmod support yes yes no no no no
    Modules to install none
    Module name n/a rewrite
    Loadmodule directive n/a LoadModule rewrite_module <module_locations>/mod_rewrite.so
  2. Open VirtualHost config that you want to set up the redirection from using your favorite text editor.
    $ sudo vi /etc/apache2/sites-enabled/000-default.conf
  3. Add redirect directive within VirtualHost configuration.
    <VirtualHost *:80>
        ServerName simplified.guide
        Redirect permanent / http://www.simplified.guide/
    </VirtualHost>

    permanent is equivalent to 301 redirect and you can use temporary instead for 302 redirect.

  4. Save and close the file.
  5. Restart Apache for the changes to apply changes.
    $ sudo systemctl restart apache2 # Ubuntu, Debian
    $ sudo systemctl restart httpd # CentOS and Red Hat
Discuss the article:

Comment anonymously. Login not required.