Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirect to HTTPS with .htaccess with a specific domain

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

like image 993
Dan Stern Avatar asked Aug 06 '12 20:08

Dan Stern


Video Answer


3 Answers

You could simply add another RewriteCond to check if the host is metrikstudios.com

RewriteCond %{HTTP_HOST} ^metrikstudios\.com [NC]

and it should look like this:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^metrikstudios\.com [NC]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI}
like image 95
Fabian Avatar answered Oct 06 '22 09:10

Fabian


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]
like image 36
Amit Verma Avatar answered Oct 06 '22 09:10

Amit Verma


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.

like image 25
K. Igor Avatar answered Oct 06 '22 08:10

K. Igor