Redirect non-www to www .htaccess

This guide will show you how to redirect your website URL from non-www to www and vice versa.

If you have cPanel on your web hosting account, I’d recommend in reaching out to your host to get this done for you. In this guide, I’ll share code to redirect in your .htaccess.

Please be careful when working with your .htaccess file as it can bork your site.

Your .htaccess file is found in the root directory of your web hosting account usually. Edit this via FTP/Control Panel with your text editor and add the following code to your .htaccess:

Redirect non-www to www

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

Redirect www to non-www

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

This will create a 301 redirect for all traffic from www.yourwebsite.com to https://yourwebsite.com and vice-versa.

Photo by Airam Dato-on on Unsplash


Leave a Reply

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

*

*

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.

For official information about COVID-19, visit https://sacoronavirus.co.za