Learn how to set up a 301 redirect to force non-WWW URLs using the .htaccess file, ensuring domain consistency and improving SEO performance.
In today’s competitive digital landscape, ensuring that your website maintains a consistent domain format is crucial for SEO and optimization. One effective way to achieve this is by redirecting the “www” version of your domain to the non-WWW version. This ensures all visitors land on the same version of your site, preventing duplicate content issues and improving SEO performance.
Preparation:
Before you start, make sure you have access to your website’s root .htaccess file. This is where you’ll implement the necessary changes to redirect the “www” version of your domain to the non-WWW version.
Step-by-Step Guide:
1. Locate the .htaccess File:
The .htaccess file is typically located in your website’s root directory. If you don’t see it, ensure that hidden files are visible in your file manager, as it may be hidden by default. If the file doesn’t exist, you can create a new one using a text editor.
2. Backup the .htaccess File:
Before making any changes, always back up your .htaccess file. This allows you to restore the original version if something goes wrong during the process.
3. Insert the Non-WWW Redirect Code:
Open the .htaccess file with a text editor and insert the following code snippet to redirect all “www” traffic to the non-WWW version of your domain:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.site.com [NC]
RewriteRule ^(.*)$ http://site.com/$1 [L,R=301]
</IfModule>
Note: Replace site.com with your actual domain name.
Code Breakdown:
< IfModule mod_rewrite.c >: This checks whether themod_rewritemodule is enabled on your Apache server. If it is, the following rewrite rules will be executed.RewriteEngine on: This enables the Apache rewrite engine, allowing you to define rules that modify URL requests.RewriteCond %{HTTP_HOST} ^www\.site\.com [NC]: This condition checks if the requested domain starts with “www” (case-insensitive). If it does, the rule that follows will be applied.RewriteRule ^(.*)$ http://site.com/$1 [L,R=301]: This rule redirects the “www” version of your domain to the non-WWW version, using a 301 (permanent) redirect. The$1represents the part of the URL after the domain.< / IfModule >: This ends themod_rewriteblock.
4. Save and Test the Redirect:
After adding the code, save the changes to your .htaccess file and test the redirection. Visit the “www” version of your domain in a browser (e.g., www.example.com), and it should automatically redirect to the non-WWW version (e.g., example.com).
If the redirect doesn’t work, double-check your code and ensure the mod_rewrite module is enabled on your server. You can also review your Apache error logs for any issues.
Conclusion:
Setting up a non-WWW redirect is a vital step in ensuring consistent domain name formatting, which is important for both search engine optimization (SEO) and user experience. By following this guide, you’ll be able to create a simple, effective redirect that ensures all traffic is consolidated under the non-WWW version of your domain. This not only helps prevent duplicate content issues but also aids search engines in indexing your site more efficiently.
Next: Always Redirect to WWW

