Redirect to HTTPS with .htaccess with a specific domain

Viewed 31750

I currently have 2 domains that access to the same folder on my server: metrikstudios.com and ziced.com.

I want the users that enter through http://metrikstudios.com be redirected to https://metrikstudios.com and the users that enter through http://ziced.com don't be redirected to https://ziced.com.

I currently have this on my .htaccess

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

Thanks

4 Answers

The accepted solution above only redirects a non-www domain from http to https .

If you want to redirect both www and non-www versions of your domain to ssl put the following RewriteCond right above your http to https Rule or before RewriteCond %{HTTPS} off line :

RewriteCond %{HTTP_HOST} ^(www\.)?example.com$ [NC]

Here is the complete Rule to redirect a specific domain to https.

RewriteEngine on

# First we will check the host header (url)
#if it's www.example.com or example.com
RewriteCond %{HTTP_HOST} ^(www\.)?example.com$ [NC]
# now we will check the https header
# if https is off (Is non-ssl)
RewriteCond %{HTTPS} off
#redirect the request to https
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]

Sometimes you might want to redirect only on live server and leave local settings for it as is. For example if on local machine you have registered local host named www.mysite.loc and set up local instance of project on this host.

In this case this might help someone too:

RewriteEngine On
RewriteCond %{HTTPS} =off
RewriteCond %{HTTP_HOST} !.loc$ [NC]
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]

where !.loc$ - rule to ignore redirect to https if host ends with .loc.

Related