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

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 Managed WordPress Hosting
WPEngine is the best and most secure managed 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.
Leave a comment
Comment policy: We love comments and appreciate the time that readers spend to share ideas and give feedback. However, all comments are manually moderated and those deemed to be spam or solely promotional will be deleted.