Always Redirect to WWW

Always Redirect WWW

If you have a website that uses a non-www domain name (e.g. example.com) and you want to always redirect visitors to the www version of your site (e.g. www.example.com), you can do so with Apache’s mod_rewrite module. This tutorial will guide you through the process of setting up a mod_rewrite rule that always redirects to the www version of your site.

Step 1: Open your Apache Configuration File

The first step is to open your Apache configuration file. This file is usually located in the “conf” directory of your Apache installation. Depending on your server setup, you may need to be logged in as the root user to access this file.

Step 2: Locate the Virtual Host or Directory Section

Locate the section of your Apache configuration file where you can add custom directives. This section is usually labeled “VirtualHost” or “Directory“. If you’re not sure where to put the code, consult your web hosting provider or system administrator.

Step 3: Add the mod_rewrite Code

Copy and paste the following code into your Apache configuration file, within the Virtual Host or Directory section:

# BEGIN Always WWW
<IfModule mod_rewrite.c>
RewriteEngine on  
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
</IfModule>
# END Always WWW

This code tells Apache to use mod_rewrite to always redirect visitors to the www version of your site. The RewriteCond line checks if the HTTP_HOST does not start with “www.” (which means it is a non-www domain), and the RewriteRule line redirects the visitor to the www version of the site with a 301 (permanent) redirect.

Step 4: Save and Restart Apache

Save the changes to your Apache configuration file and restart Apache for the changes to take effect. The command to restart Apache varies depending on your server setup. On a Linux system, you can usually run the following command:

sudo service apache2 restart

Step 5: Test the Redirection

Once you have restarted Apache, visit your website using the non-www domain (e.g. example.com). You should be automatically redirected to the www version of your site (e.g. www.example.com). If the redirection is not working, check your Apache error log for any error messages.

Step 6: Verify the Redirection with a Tool

To verify that the redirection is working properly, you can use a tool such as Redirect Checker. Simply enter your non-www domain (e.g. example.com) and it will show you if the redirection is working and if there are any issues.

Step 7: Consider Updating Your Links and Canonical Tags

Once you have set up the redirection, it is a good idea to update any internal links on your site to use the www version of your domain. You should also update your canonical tags to use the www version of your domain. This will ensure that all internal and external links point to the same version of your site and will prevent duplicate content issues.

Conclusion

Redirecting visitors to the www version of your site is a simple and effective way to ensure that all visitors are accessing the same version of your site. By following the steps outlined in this tutorial, you can set up this redirection using Apache’s mod_rewrite module.


Stay in the loop with our web development newsletter - no spam, just juicy updates! Join us now. Join our web development newsletter to stay updated on the latest industry news, trends, and tips. No spam, just juicy updates delivered straight to your inbox. Don't miss out - sign up now!


We’ve tried our best to explain everything thoroughly, even though there’s so much information out there. If you found our writing helpful, we’d really appreciate it if you could buy us a coffee as a token of support.

Also, if you’re interested in learning more about WordPress, Javascript, HTML, CSS, and programming in general, you can subscribe to our MailChimp for some extra insights.

Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.