Redirecting traffic from a non-www domain to a www domain ensures consistency in how users access your website. This practice can also prevent duplicate content issues, which is important for search engine optimization (SEO). Apache HTTP Server offers straightforward methods to achieve this redirection.
Apache’s mod_rewrite module is the most commonly used tool for URL redirection. This module allows you to direct traffic based on conditions, such as routing all non-www requests to the www version of your domain. These configurations can be implemented either in the main Apache configuration file, within a VirtualHost, or using a .htaccess file, depending on your server setup.
By setting up these redirections, you create a consistent and reliable user experience. Users who access your domain, whether with or without the www prefix, will be directed to the same website. This approach is simple to implement and helps maintain a clean URL structure.
Methods to redirect domain and URL in Apache:
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.
- 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 - Open or create a .htaccess file on the web folder where you want to set the redirection from.
$ sudo vi /var/www/html/.htaccess
- 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.
- Save and close the file.
- 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.
- 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 - 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
- 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.
- Save and close the file.
- Restart Apache for the changes to apply changes.
$ sudo systemctl restart apache2 # Ubuntu, Debian $ sudo systemctl restart httpd # CentOS and Red Hat
Mohd Shakir Zakaria is an experienced cloud architect with a strong development and open-source advocacy background. He boasts multiple certifications in AWS, Red Hat, VMware, ITIL, and Linux, underscoring his expertise in cloud architecture and system administration.
Comment anonymously. Login not required.