Directing your users from a naked domain (e.g., example.com ) to a www-prefixed domain (e.g., www.example.com ) is a common practice in web management. It ensures consistency in the URL users interact with, and, when configured correctly, it can have SEO benefits as well.

The Apache HTTP server provides a straightforward way to perform this redirection. The mod_rewrite module, bundled with Apache, enables web administrators to redirect users based on conditions. The most common use-case is to redirect traffic from the naked domain to its www equivalent.

If you have a virtual host file for your domain, you can add a few lines to achieve this redirection. If not, these configurations typically reside in the main Apache configuration file or the .htaccess file in your website's root directory.

Redirect naked domain to www in Apache using htaccess

  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

  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.