.htaccess not redirecting HTTPS to HTTPS - .htaccess

I have a website with SSL. The redirect from http to https seems to work fine. my problem is how should I redirect the https://example.com to https://www.example.com.
I have tries this but it's not working.
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} (.*)
RewriteRule .? https://%1%2 [R=301,L,U,NE]

RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

Related

htaccess redirect from https to http doesn't work

I'd like to redirect all my website URLs(with www) from https to http
Now with the code below it works only from https://www.example.it to http://www.example.it (and all other pages, for example: example.com/page02.php)
But doesn't work from https://example.com to http://example.com (ERR_CONNECTION_REFUSED)
This is the code I have:
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^example.com [OR]
RewriteCond %{HTTP_HOST} ^www.example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
Does someone know how do I do?
Try this :
RewriteCond %{SERVER_PORT} ^443
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Put rules after RewriteEngine On and clear browser cache then test

Redirect NON-www to www for https

I've the following redirect
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
How can I redirect non WWW https to WWW?
The htaccess is located in the public_html folder
You can use the following :
RewriteCond %{HTTPS} !on [OR]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule (.*) https://www.example.com%{REQUEST_URI} [L,R=301]
This will redirect
http://example.com
https://example.com
to
https://www.example.com
I'm using this on a Joomla site and it redirects everything to https://www
I can't see this solution anywhere else so thought I'd share it
RewriteCond %{HTTP_HOST} ^domainname\.co.uk [NC]
RewriteRule ^(.*)$ https://www.domainname.co.uk/$1 [R=301,L]

Redirecting to https and subdomain to http

I have a website which uses an SSL cert on production. At the same time, a staging version (staging.domain.com) should only resolve to http.
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.in$ [NC]
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
I know it can be done using htaccess, but I simply cannot get it to work.
www.domain.com / domain.com should redirect to https
staging.domain.com should redirect to http

How to stop HTTPS redirection for a subdomain

Currently, I have following redirection setup in htaccess to redirect www and non-www URL to HTTPS with www.
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule ^(.*) https://www.example.com/$1 [R=301,L]
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
I have now created a subdomain dev.example.com. But when I run it, site redirects to HTTPS with www. How can I stop it, and load dev without any redirection?
Add another condition to the HTTPS rule:
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule ^(.*) https://www.example.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} !dev\.example\.com$ [NC]
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

htaccess - redirect non-www to https with www

I recently switched from a windows server to godaddy,linux,cpanel server. I'm not goos with htaccess and I think this should be a rather simple thing to do but I can't get it right.
My SSL Certificate is set up to work on the site like this: https:www.mydomain.com but not for the (non-www site).
I'd like all traffic coming to https:example.com/any-folder/any-file redirected to https:www.example.com/any-folder/any-file
all traffic coming to http:example.com/any-folder/any-file redirected to http:www.example.com/any-folder/any-file
Thanks for any help you can provide.
This is my htaccess file now: (all request are getting redirected to https.... I'm confused)
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]
The reason for this behavior is that
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
will rewrite all request that lack www. to http://www.example.com/$1. Also for HTTPs requests. This rewrite will result in a new request, since it is a redirect, what you've defined with [R].
After that, the next rule
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]
will redirect all request that are not HTTPs to https://www.example.com/$1. Even if the original request was not supposed to be HTTPs.
You have to do it somehow like this:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS} on
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]

Resources