htaccess redirect to HTTPS - .htaccess

I've been trying to figure out a way to redirect only one domain to HTTPS but haven't found a good solution. I was able to redirect all requests to https by using the HTTPS!=on condition but I host multiple domains and only one has SSL.
This gave me some success.
RewriteCond %{HTTP_HOST} ^(127\.0\.0\.1|localhost)$
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
But it doesn't seem to redirect URLs like www.mydomain.com/order/ and mydomain.com/order/
Basically it works only for the main page currently at www.mydomain.com or mydomain.com.
What am I missing?

Your problem is this:
RewriteCond %{HTTP_HOST} ^(127\.0\.0\.1|localhost)$
That says, "Only use the next RewriteRule if the host is 127.0.0.1 or localhost, exactly." If you host mydomain.com on that same server, it won't match. You need to add the name of the domain you want to redirect. Example:
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.com$
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

Related

Domain too many redirection

Hi after 1 week of research i'm still stuck with my domain redirections
I want my http://domain.fr pointing to https://www.domain.fr
I'm trying with htaccess file with no success...
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^www\.*
RewriteRule (.*) https://%{HTTP_HOST}/$1 [R=301,L]
When i'm testing my redirections it seems https://www.domain.fr is redirecting itself without understading why...
have you guys any idea ?
The problem is in your 2nd RewriteRule
RewriteCond %{HTTP_HOST} ^www\.*
RewriteRule (.*) https://%{HTTP_HOST}/$1 [R=301,L]
You rule does a permanent redirection of www domain to the same location ie : ( www.e ample.com => www.example.com ) that is why you got the redirect loop error.
To redirect http urls to https ,first of all you need to check if the request scheme is http. You can use RewriteCond %{HTTPS} off to check the non - secure http connection and then use RewriteRule to redirect the request to https .
Use the following simple Rule to redirect http to https :
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]
Make sure to clear your browser cache before testing this.

https for main domain and http for subdomain

So i need very simple codes for .htaccess
I want to redirect all traffic to https in main domain and redirect all traffic to http in subdomain without WWW.
Right now i am using this code to redirect all traffic to https
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
So main domain should be like this: https://website.com
and subdomain should be like this: http://sub.website.com
I found many answers but very complicated, so need simple solution.
EDITED
Solution given by anubhava works great. Thanks for your solution.
You can use:
RewriteEngine On
# maindomain -> https
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(website\.com)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R,L]
# subdomain -> http
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^(?:www\.)?(sub\.website\.com)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R,L]

Forcing SSL only on the main domain on cPanel

I currently have 3 domains on my cPanel account with HostGator. I am trying to force the main domain to use HTTPS, but all other domains to use HTTP. When I add the following to the main directories .htaccess file, it forces it upon all sub-directories too.
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
Is there any way to not make it do this on the other domains?
You just need another RewriteCond and check for the HTTP_HOST so that the RewriteRule is not applied to domains that don't need to have https:
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^no-https-needed.example.com$
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

Reditect main domain to https, but not subdomain

I have a website say example.com and a SSL for www.example.com.
I want - if anyone goes to example.com or www.example.com, he is redirected to https://www.example.com/.
But if he goes to sub.example.com, he should not be redirected to https, i.e. stay on to http.
I've gone through various answers on StackOverflow, but could not configure it out. Currently, I'm using the following:
RewriteCond %{HTTPS} !^on$
RewriteRule (.*) https://www.example.com/$1 [R,L]
This redirects sub.example.com to https://www.example.com/sub/, which I don't desire to.
Please help!
You want to add an extra condition that checks the current hostname. You can do this with the %{HTTP_HOST} variable.
RewriteCond %{HTTPS} !^on$
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule (.*) https://www.example.com/$1 [R,L]

Remove SSL integration from a specific folder using htaccess

I have an integrated SSL for my entire site and have placed htaccess code to redirect to https when anyone visits my domain URL. But I want to keep one folder out of this redirection to https. Please help me with this... Below is the htaccess code placed in my root to redirect all requests to https counterpart
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
Thanks
Just add a condition to exclude the folder:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} !^/folder1
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
And if you wanted to redirect SSL requests to non-SSL for /folder1, then:
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} ^/folder1
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Resources