HTTP and HTTPS w/ www Redirect via .htaccess

, ,

So every now and then I run into an issue where I need to setup redirects for addon domains.  Here is one method that has always worked for me.  Adding it in this way allows for https and non http and also handles non-www and www to redirect to the www.newdomain.com url.  Also looks for the non-www of newdomain.com and redirects to www.

RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^olddomain\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.olddomain\.com$ [OR]
RewriteCond %{HTTP_HOST} ^newdomain\.com$
RewriteRule ^(.*)$ "https\:\/\/www\.newdomain\.com\/$1" [R=301,L]

RewriteCond %{HTTPS_HOST} ^olddomain\.com$ [OR]
RewriteCond %{HTTPS_HOST} ^www\.olddomain\.com$ [OR]
RewriteCond %{HTTPS_HOST} ^newdomain\.com$
RewriteRule ^(.*)$ "https\:\/\/www\.newdomain\.com\/$1" [R=301,L]

 

 

So if you don't need to setup domains to forward when an addon domain is used you can use the following as it is based off just HTTPS.

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

or

RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]

 

 

Skills

Posted on

February 13, 2019

Submit a Comment

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