If you have a website that uses a non-www domain name (e.g., example.com) and you want to redirect visitors to the www version (e.g., www.example.com), you can achieve this using Apache’s mod_rewrite module. This tutorial will guide you through the process of setting up a mod_rewrite rule to ensure all visitors are directed to the www version of your site.
Step 1: Open Your Apache Configuration File
The first step is to locate and open your Apache configuration file. This file is typically located in the conf directory of your Apache installation. You might need root access to modify this file, so ensure you are logged in as the root user or have appropriate privileges.
Step 2: Locate the Virtual Host or Directory Section
Next, find the section of your Apache configuration file where you can add custom directives. This is usually found inside a VirtualHost or Directory block. If you’re unsure where to place the code, consult your web hosting provider or system administrator for guidance.
Step 3: Add the mod_rewrite Code
Within the VirtualHost or Directory section of your Apache configuration file, add the following code to redirect all non-www traffic to the www version of your domain:
<ifmodule mod_rewrite.c>
RewriteEngine on<br />
RewriteCond %{HTTP_HOST} !^www.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
</ifmodule>
Code Breakdown:
IfModule mod_rewrite.c: This checks if themod_rewritemodule is enabled. If it is, the rules inside this block will be executed.RewriteEngine on: Activates the URL rewriting engine, which allows Apache to redirect URLs based on defined rules.RewriteCond %{HTTP_HOST} !^www\.: This condition checks if the incoming request’s domain does not start with “www”. The!negates the match, meaning this condition applies only to non-www domains.RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]: This rule redirects all traffic to the www version of the domain, with a 301 (permanent) redirect status. TheR=301flag ensures that the redirect is permanent, and theLflag indicates that this is the last rule to process.
Step 4: Save and Restart Apache
After making changes to the Apache configuration file, save the file and restart Apache to apply the changes. The command to restart Apache may vary based on your server setup, but on most Linux systems, you can use the following command:
sudo service apache2 restart
Step 5: Test the Redirection
Once Apache has restarted, visit your website using the non-www version of your domain (e.g., example.com). If the redirect is correctly configured, you should be automatically redirected to the www version (e.g., www.example.com).
If the redirection does not work as expected, check your Apache error logs for any issues, or ensure that the mod_rewrite module is enabled on your server.
Step 6: Verify the Redirection with a Tool
To confirm that the redirection is working properly, you can use an online tool like Redirect Checker. Enter your non-www domain (e.g., example.com), and the tool will show whether the redirection is functioning correctly and if there are any issues to address.
Step 7: Update Your Internal Links and Canonical Tags
After setting up the redirect, it’s a good idea to update any internal links on your site to use the www version of your domain. Additionally, ensure that your canonical tags are updated to reflect the www version. This helps search engines and users consistently access the correct version of your site, reducing the potential for duplicate content issues.
Conclusion
Redirecting all visitors to the www version of your site is a simple and effective way to ensure consistency across your domain and improve SEO. By following the steps outlined in this guide, you can easily implement this redirect using Apache’s mod_rewrite module. Once set up, your visitors and search engines will always be directed to the correct version of your domain.

