Force Redirect to non WWW

Force Redirect to non WWW

In today’s competitive digital landscape, ensuring that your website has a consistent domain name format is vital for optimization and SEO success. One effective way to achieve this is by redirecting the “www” version of your domain to the non-WWW version.

Preparation:

Before diving in, confirm that you have access to your website’s root .htaccess file. You’ll need to edit this file to create the redirect.

Step-by-Step Guide:

1. Locate the .htaccess file:
Typically, the .htaccess file is in your website’s root folder. If you can’t find it, go ahead and create a new one.

2. Backup the .htaccess file:
As a safety measure, always create a backup of your .htaccess file before making changes. This allows you to quickly revert to the original version if any issues arise.

3. Insert the non-WWW redirect code:
Open the .htaccess file with a text editor and add the following code snippet:

# BEGIN Force non-www
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.site\.com [NC]
RewriteRule ^(.*)$ http://site.com/$1 [L,R=301]
</IfModule>
# END  Force non-www

Code Breakdown:

<Ifmodule mod_rewrite.c> checks if the mod_rewrite module is enabled on your Apache server.

RewriteEngine on activates the rewrite engine.

RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC] is a condition that matches the “www” version of your domain name, without case sensitivity (NC stands for No Case).

RewriteRule ^(.*)$ http://%1/$1 [L,R=301] is the rewrite rule that redirects the “www” version to the non-WWW version using a 301 (permanent) redirect status.

</Ifmodule> ends the conditional block.

4. Save and test the redirect:
After incorporating the code into your .htaccess file, save the changes and test the redirect by accessing the “www” version of your domain. If properly configured, your browser should automatically redirect to the non-WWW version.

Conclusion

Setting up a non-WWW redirect for your website is a critical aspect of website optimization and SEO. By following this detailed guide you’ll be well-equipped to create a redirect that ensures your website maintains a consistent domain name format for search engines to index effectively.


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.