Apache.htaccess convert http uri to https causing redirect loop
I know this question has been asked a thousand times, but I cannot find the answer.
We have a website hosted in 123-reg web hosting package (no access to http config files). I have added ssl to the site and the certificate works when it requests to use https directly.
The problem occurs 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 also uses port 80 (I am currently requesting this with 123-reg). The condition is always met and calls the 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 because the variable is never included (and causes a redirect loop). I wonder if it has something to do with port no = 80 for https.
I found two server variables, SSL and HTTP_X_FORWARDED_SSL, which change from "" to 1, but only when I delete the .htaccess file and request http or https directly.
If I try to use the SSL or HTTP_X_FORWARDED_SSL variables in RewriteCond it triggers a redirect loop.
I can't see the variables while the redirect loop is in progress, so I don't know if they change during the redirect.
edit:
I found the answer. I used:
RewriteCond %{SSL} !1
which should be:
RewriteCond %{ENV:SSL} !1
source to share
No one has answered this question yet
See similar questions:
or similar: