Https redirect error - .htaccess

I have an SSL certificate for website http://xyz.co/.
I created .htacces file for forcing users to use https instead of http.
I tried:-
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
But it shows me error as
net::ERR_TOO_MANY_REDIRECTS

Try to redirect to an entirely different url to make sure your condition is actually firing. At first sight I would say your RewriteRule is wrong because that's no RegEx, ^ is just an anchor.
So try this:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^.* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
I assume you don't get ERR_TOO_MANY_REDIRECTS errors when you remove this part.

Related

why is htaccess file causing too many redirects when trying to redirect to https

I am trying to update my htaccess file to redirect from http to https but I am getting too many redirects. I have never used this type of file before so not sure if doing something very obviously wrong
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule .* https://www.xxxx.org.uk%{REQUEST_URI} [L,R=301]
I have had a solution which works for those that face a similar problem in the future.
RewriteEngine On
RewriteCond %{ENV:HTTPS} !=on
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]

Issue with linking directly to my URL (https://)

I've just gotten an SSL cerificate and have redirected my site to display https:// rather than http:// using HTaccess:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://www.example.co.uk/%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Before I used to link to my index like this:
Home
And you would be taken to http://www.example.co.uk.
Now with the SSL I've changed it to this:
Home
And it is taking me to https://www.example.co.uk/www.example.co.uk/.
Any idea why this is?
Your http -> https rules should be this:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
Make sure to clear your browser cache or use a new browser for testing.
This is what you need
<IfModule mod_rewrite.c>
RewriteEngine On
# Redirect HTTP to HTTPS automatically (only if not in localhost)
RewriteCond %{HTTP_HOST} !=localhost
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>

.htaccess How to Exclude One Site From SSL?

I'm trying to exclude one site from using SSL, but I can't seem to get the directive right. I want all my AddOn domains to use SSL except example.com. (because StartSSL is difficult to get along with)
# Enable https
RewriteCond %{HTTPS} off
#RewriteRule (.*) https://%{HTTP_HOST}:443%{REQUEST_URI}
RewriteRule !^example.com(/|$) https://%{HTTP_HOST}:443%{REQUEST_URI}
What you're trying to do will not work. The SSL handshake is initialized before any request data is sent to the server, therefore, you're going to get a certificate exception if someone goes to https://example.com no matter what you do.
If the certificate exception isn't your concern, then you need to match against the %{HTTP_HOST} variable:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^(www\.)?example\.com$ [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
then if you need:
RewriteCond %{HTTPS} on
RewriteCond ^{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteRule ^(.*)$ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Force all requests to use https:// and prepend www

I have spent countless hours trying to solve this problem and it's doing my head in. I'm sure that it is very simple but none of the answers I have found either on stackoverflow.com or other sites work for me.
I am using an SSL certificate that is only valid for www.example.com and for a myriad of silly corporate reasons I am stuck with only this certificate.
I would like to force all requests to use SSL and prepend www. to the domain. All of the following domains rewrite correctly:
example.com/
http://example.com/
http://www.example.com/
https://www.example.com/
The domain that is not rewritten and is the one problem I cannot find the answer to anywhere is
https://example.com
It simply spits out the usual "Certificate Is Not Valid" warning.
I have the following code in my .htaccess file:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]
RewriteCond %{http_host} ^example.com [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
I really hope that somebody has a solution for me because I cannot find the answer anywhere.
This should work:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=302]
RewriteCond %{HTTPS} =off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NC,R=302,L]
It will check to see if it has www in the url, if it doesn't then it will add it. Then it will check and see if it is https and if it isn't then it will add it.

htaccess, Always https://www

Currently using the following to make sure my website always is using www. How would I modify this to be www and https?
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Tried this, but ended up with www.www.website if the user had no https but did already have a www
RewriteCond %{HTTPS} off
RewriteRule ^(.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Not sure how to combine the two
Make us of {SERVER_PORT} to see if the request is made via regular connection (80), then redirect to the https URL.
This should do it:
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]
Here are some nice example, using different methods.

Resources