Apache .htaccess convert http uri to https causing

2019-05-23 02:07发布

问题:

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