Apache .htaccess convert http uri to https causing redirect loop - apache

I know this question has been asked a thousand times, but I cannot seem to find the answer.
We have a website hosted by 123-reg's shared web hosting package (no access to http config files). I have added ssl to the site, and the certificate works when directly requesting using https.
The problem arises when I try to redirect everything from http to https using the .htaccess file.
First I tried the SERVER_PORT variable in the condition:
RewriteEngine On
RewriteBase /
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://example.co.uk/$1/ [R=301,L]
This does not work as the https redirect request uses port 80 also (I am querying this with 123-reg at the moment). The condition is always met and causes a redirect loop.
Next I tried the HTTPS variable:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} !on
RewriteRule ^(.*)$ https://example.co.uk/$1/ [R=301,L]
This condition is always met as the variable is never set to on (and causes a redirect loop). I wonder if this is to do with the port no = 80 for https.
I found two server variables, SSL and HTTP_X_FORWARDED_SSL, which do change from "" to 1, but only when I delete the .htaccess file and directly request http or https.
If I try and use the SSL or HTTP_X_FORWARDED_SSL variables in the RewriteCond condition, it causes a redirect loop.
I cannot see the variables while the redirect loop is happening, so I do not know if they are being changed during the re-direct.
edit:
I have found the answer. I was using:
RewriteCond %{SSL} !1
which should be:
RewriteCond %{ENV:SSL} !1

Related

Web server keeps redirecting to HTTP

I'm trying to use CloudFlare page rule to redirect to https but when I set it up it gives me the redirect loop and keeps changing back and fourth between http and https. I deleted my .htaccess file and deleted every redirect I had in the control panel. Still not working. Neither my webhost support or CloudFlare support can help. This is my page rule setup as CloudFlare support told me to set it:
URL pattern: http://.kohlercoding.dk/
setting: always use https
If you can edit the .htaccess file try this:
RewriteCond %{HTTP_HOST} ^website.com [NC]
RewriteRule ^(.*)$ https://www.website.com/$1 [L,R=301]
RewriteCond %{HTTPS} =off
RewriteCond %{HTTP_HOST} ^www.website.com$
RewriteRule ^(.*)$ https://www.website.com/$1 [R=301,L]
I just found out the problem is that I had my SSL setting on "Full" on CloudFlare. That means I have to have a verified certificate on my webhost which I didn't. That's why it didn't work. So changing it to "Flexible" fixed my problem. Or having a self-signed certificate on the webhost if they support it and, unlike UnoEuro, doesn't make you pay for HTTPS Protection.

How to redirect a page both to https and to the 'www' version of the site

I recently installed my SSL certificate, and I'm attempting to enforce a https connection to all my pages. However, previously I also redirected all requests to the www version of the request page. When combining an http redirect to https and concurrently redirecting traffic to www, I get a looping redirect warning on browsers. Hence, how can I make .htcaccess rule (I actually just use the directory config file) that will achieve what i want: always https://'www'
Here's the current combination that I have:
RewriteEngine on
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^/(.*) https://%{HTTP_HOST}$1 [NC,R=301,L]
RewriteCond %{HTTP_HOST} ^mydomain.com
RewriteRule (.*) https://www.mydomain.com$1 [R=301,L]
Your question is a duplicate of htaccess redirect to https://www
Also, you can solve the WWW problem with DNS by simply pointing your naked domain to WWWizer's free naked domain redirect service's IP 174.129.25.170

Difference between 2 apache mod_rewrites

I've found 2 different code snippets to force https on my website:
RewriteRule (.*) https://%{SERVER_NAME}/$1 [R,L]
and
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
I'm sure that they both work (one's from Httpd Wiki and the other's from SSL shopper). Would someone be able to explain the differences in how they perform the redirect?
They just use different Apache variables to make up the URL for redirect.
RewriteRule (.*) https://%{SERVER_NAME}/$1 [R,L]
This first rule takes the filename if one is entered such as myfile.php and appends the redirect with it replacing $1 in the redirect so that you get https://somesite.com/myfile.php
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
The 2nd one using %{HTTP_HOST} will grab the information from the http headers instead to make up the URL used to redirect so entering the same url http://somesite.com/myfile.php will be redirected to https://somesite.com/myfile.php
It's just a matter of telling apache what to use for redirection. Either use the server internal name or use the one sent by the browser.
%{SERVER_NAME}
That is a server internal variable in apache and is defined in the server config.
%{HTTP_HOST}
This is the what is sent by the browser in the HTTP request headers. This is client side while the SERVER_NAME if from the server config.
%{REQUEST_URI}
REQUEST_URI is the path component of the requested URI, such as "/index.html". This is a special Apache variable.
There a many ways that have been done to redirect to https and both should work. Your choice.

Redirect Loop while redirecting all http requests to https using .htaccess

I have the following rules on my .htaccess file
# to redirect http to https
RewriteCond %{HTTPS} off
RewriteRule (.*) https://www.example.com/$1 [R=301,L]
# to redirect urls with index.php to /
RewriteCond %{THE_REQUEST} ^.*/index.php
RewriteRule ^(.*)index.php$ /$1 [R=301,L]
# to redirect non www requests to www url
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
When I am trying to access the website, it turns into a Redirect Loop. How to fix this issue and redirect properly?
Just in case somebody have redirect loop when using Apache http->https rewrite behind load balancer, here's solution that worked for me.
I had the same problem when used RewriteCond %{HTTPS} off for Apache behind load balancer, when load balancer does SSL stuff.
If https version of the site is not configured via Apache ModSSL it doesn't set %{HTTPS} variable to "on" and keeps redirecting infinitely.
The simplest solution to fix it is to target all https traffic to another Apache VirtualHost (when SSL is handled by load balancer) that is the copy of main one, but has different port (lets say 81). And in .htaccess do mod_rewrite for everything that is not on port 81:
ReWriteCond %{SERVER_PORT} !^81$
RewriteRule ^/(.*) https://%{HTTP_HOST}/$1 [NC,R,L]
The second way to do this is to send X-Forwarded-Proto header from load balancer to Apache and use it in rewrite condition:
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
I've seen a lot of people suffering redirect loops when trying to use .htaccess files to move from http to https. And there are a LOT of different answers to how to solve this issue. Some people say:
ReWriteCond %{SERVER_PORT} 80
OR
RewriteCond %{HTTPS} off
OR
RewriteCond %{HTTPS} !on
OR (as above)
RewriteCond %{HTTP:X-Forwarded-Proto} !https
OR EVEN
RewriteCond %{HTTP:X-Forwarded-SSL} =off
but none of these worked for me. I eventually discovered the underlying truth, that the different servers out there are configured in different ways, and they're all providing different server variables.
If none of the above work for you, then the trick is to use PHP to find out what env variables your particular server is sending you when you access an http page, and what env variables it sends you when you access an https page, and then you can use that variable to do the redirect. Just make a PHP file (such as showphpvars.php) on your server with this code:
<?php phpinfo() ?>
and then view it with a browser. Find the section of variables with _SERVER["HTTP_HOST" (etc)] in it, and have a scout around for one that changes for http versus https. Mine turned out to be a variable called SSL that was set to 1 when using https, and not set at all when using http.
I used that variable to redirect to https with PHP, which is so much nicer than using htaccess, but I think that any of the _SERVER variables can also be accessed using htaccess, if you're keen to continue to use that. Just use the name inside the quotes, without the _SERVER[""] bit that PHP adds.
For your information, it really depends on your hosting provider. It may be using a Load Balancer, as stated by Konstantin in another answer.
In my case (Infomaniak), nothing above actually worked and I got infinite redirect loop.
The right way to do this is actually explained in their support site:
RewriteEngine on
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule (.*) https://your-domain.com/$1 [R=301,L]
So, always check with your hosting provider. Hopefully they have an article explaining how to do this. Otherwise, just ask the support.
If you get a redirect loop no matter what you do in htaccess, do the redirect in PHP instead.
I used phpinfo(), like #z-m suggests, to find the variable that changes when I'm on SSL. In my case it was $_SERVER['HTTP_X_PROTO'] == "https". When not on SSL, this variable is not set.
This is the code I use to redirect from HTTP to HTTPS:
if ($_SERVER['HTTP_X_PROTO'] != "https") {
header("HTTP/1.1 301 Moved Permanently");
$location = "https://" . $_SERVER[HTTP_HOST] . $_SERVER[REQUEST_URI];
header("Location: $location");
exit;
}
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{
In my case it was:
if ($_SERVER['HTTPS'] != "on")

.htaccess Redirect and proxy ports

I have an apache server that is part of a cloud servers network.
The virtual server its configured to respond at port 9000, but you access from outside through port 80. Thats working fine, except for when I try to make a redirect from the .htaccess file
I have the next 2 lines to redirect errors to a folder index
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f
RewriteRule .* /folder/ [R=301,L]
That tries to redirect the user to:
www.domain.com:9000/folder/, and it should be redirecting to www.domain.com/folder (which means, www.domain.com:80)
How can I avoid that?
thanks!
I solved it changing the rewrite rule to:
RewriteRule .* http://%{HTTP_HOST}/folder/ [R=301,L]