How to use .htaccess to redirect www URLs to non-www URLs
Table of Contents
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 providerBefore 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.