How to force ssl with htaccess? - apache

How to force SSL with htaccess
With this code i get a redirect error in chrome:
RewriteEngine On
RewriteCond %{HTTPS} !^on$ [NC]
RewriteRule . https://%{HTTP_HOST}/%{REQUEST_URI} [L] HTTPS_HOST ???
Thanks

You can use that:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]

I suggest you use this code and its working fine
#Force SSL
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

Related

How to rewrite rule for all https trafic using htacces except one subdomain

I am using this htaccess code for redirect all http to https:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
It works for:
www.example.com
www.domain1.example.com
www.nohttps.example.com
So, I don't want to use this rule for www.nohttps.example.com how can I do this? I dont know.
thank you for any help.
Add a condition to exclude a subdomain:
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} !^(?:www\.)?nohttps\. [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
RewriteCond %{HTTPS} =on
RewriteCond %{HTTP_HOST} ^(?:www\.)?nohttps\. [NC]
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
Make sure to use a new browser to test this change.

Redirect http to https and www to non-www?

I searched in Google for this and
checked over 50 posts but none seems to work. Lets assume that I have a domain example.com. What I want to do is redirect
http://www.example.com
https://www.example.com
http://example.com
to
https://example.com
Can anybody please help me??
Any help would be appriciated.
Note: I'm using cloudflare and I don't have access to conf file. I have access to .htaccess files.
EDIT1:My htaccess looks like
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.*)$ [NC]
RewriteRule (.*) https://%1%{REQUEST_URI} [L,R=301]
You can try something like this:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,R=301,NE]
You can use this rule in Cloudfare:
RewriteEngine On
RewriteCond %{HTTP:CF-Visitor} '"scheme":"http"' [OR]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,R=301,NE]

.htacess redirect for ip to https doesnt work well

I have tried to move my wordpress website to https.
I have dedicated ip also first i had this code below. But did'nt resolve my problem exactly
RewriteEngine On
RewriteCond %{HTTP_HOST} ^11\.11\.11\.11$ [NC,OR]
RewriteCond %{HTTP_HOST} ^([a-z.]+)?example\.com$ [NC]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
Later on I had this code which resolved url redirection problems But later on ı had the same problems about when somebody cliks a http link given before doesnt go to https url.
<IfModule mod_rewrite.c>
RewriteEngine on
#First rewrite any request to the wrong domain to use the correct one (here www.)
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
#Now, rewrite to HTTPS:
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>
And another question is how i will redirect ip to https? And where i must put that?
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
should indeed work
I used a plugin called Really simple SSl
And that entered this code.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTPS} !=on [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
</IfModule>
and before all (as mentioned by Michael Berkowski )
RewriteCond %{HTTP_HOST} 11\.11\.11\.11
RewriteRule ^(.*) https://www.example.com/$1 [L]
thanks to participants

Keep HTTP or HTTPS on .htaccess

I have the following .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_URI} /api/profile/
RewriteRule ^(.*)$ http://whatsee-profile.s3.amazonaws.com/$1 [R,L]
It redirects my fake links to the real ones and works great! But I need to keep the original HTTP or HTTPS protocol before the url. I've tried the following, but it doesn't work.
RewriteEngine On
RewriteCond %{REQUEST_URI} /api/profile/
RewriteRule (http|https):\/\/whatsee.plugapps.net\/api\/profile\/(.*) $1://whatsee-profile.s3.amazonaws.com/$2 [R,L]
Any idea what is wrong? Thanks for your help!
It exists several ways of doing it.
Here is one working solution
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteRule ^ - [env=protoc:https]
RewriteCond %{HTTPS} off
RewriteRule ^ - [env=protoc:http]
RewriteCond %{REQUEST_URI} /api/profile/
RewriteRule ^(.*)$ %{ENV:protoc}://whatsee-profile.s3.amazonaws.com/$1 [R,L]

htaccess - redirect only sub domain http to https

I want to redirect http to https with .htaccess rewrite_mod only pages under one subdomain.
for example: only for this sub domain: http://www.test.com/product/
http://www.test.com/product/good1/ to https://www.test.com/product/good1/
from http://www.test.com/product/good2/ to https://www.test.com/product/good2/
the question is: is it possible? if yes, could you give me one example?
thanks
Try this rule
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} !=on
RewriteCond %{REQUEST_URI} ^/product [NC]
RewriteRule ^ https://www.test.com%{REQUEST_URI} [R=301,L]
Try this,
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www.test.com$
RewriteCond %{REQUEST_URI} ^/product/(.*)$
RewriteRule ^(.*)$ https://www.test.com/$1 [L,R=301]