Redirect to HTTPS with .htaccess with a specific domain
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
.
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]
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}