How To Redirect Your Website Using 301 Redirects

SkyStra
Jan 30, 2020

Non-Secure Redirects

non-www to www

Google Chrome recently started hiding the www portion of the URL, but if you want your website to work via http://www.yourdomain.com instead of http://yourdomain.com, you can achieve this via 301 redirect. You will need to add following code to the beginning of your .htaccess file:

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

Make sure to replace yourdomain.com with your actual domain name. Once you save the file, your website will be automatically redirected from http://yourdomain.com to http://www.yourdomain.com.

www to non-www

To redirect your website from http://www.yourdomain.com to http://yourdomain.com, input following code to the beginning of your .htaccess file:

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

Secure Redirects

Redirect Entire Domain to HTTPS

RewriteEngine On 
RewriteCond %{HTTPS} off 
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Redirect Entire Domain from HTTP to HTTPS & non-www to www

RewriteEngine On 
RewriteCond %{HTTP_HOST} !^www\. [NC] 
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301] 
RewriteCond %{HTTP:X-Forwarded-Proto} !https 
RewriteCond %{HTTPS} off 
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

In all cases, make sure to always place the code at the beginning of your .htaccess file in public_html directory.