How to use .htaccess to redirect www URLs to non-www URLs

How to use .htaccess to redirect www URLs to non-www URLs featured image

Reading Time: 1 min, 23 sec

In some websites, URLs begin with www. while this is not the case in many websites. There is no difference between the two, but it is recommended to choose one format. They are both valid and work the same way, but it is a matter of preference. None has an SEO advantage over the other. So, you should choose anyone and stick to it.

You can use the .htacess file to redirect all requests from www to non-www. You just need to add a few lines in the .htaccess file. .htaccess can be found in the root directory of your website.

Best WordPress Hosting

Bluehost is one of the best and secure WordPress hosting provider

Before making any changes to the .htaccess file, you need to first take a backup of the .htaccess file. You can either download the file or make a copy of it.

Redirect WWW URLs to non-WWW URL

Add the following code for redirecting WWW URLs to non-WWW URLs:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.
RewriteRule ^(.*)$ https://domain.com/$1 [R=301,L]

Replace https://domain.com with the URL of your website.

Redirect non-WWW URLs to WWW URL

Add the following code for redirecting non-WWW URLs to WWW URLs:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

After making the changes, you should monitor your website to ensure the redirection is working file. If you notice any issues, you need to contact a professional developer for assistance.

Wrap UP

Redirecting non-WWW URLs to WWW URLs or WWW URLs to non-WWW URLs is simple but important. I have added ways for both redirections. So choose one URL format and make changes accordingly.

Deepanker profile image

Written by Deepanker

Deepanker Verma is an experienced WordPress developer who has been working on WordPress for more than 12 years. On TheWPGuides, he writes about WordPress, WordPress development, and WordPress plugins.

Leave a Reply

Your email address will not be published. Required fields are marked *

Need a Hand with Your WordPress Site?

Don't let technical issues slow you down. Our professional WordPress maintenance service handles updates, security, and performance optimization so you can focus on what you do best: creating content.

Learn More About Our Services