0

I have problem with http to https redirection using .htaccess

I use this code on .htaccess

    RewriteEngine On
    RewriteCond %{HTTPS} off
    RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

I have tested and used before on other servers, and it worked fine

But here on this server it fails, when I try to visit this site (http or https) I doesn't work, it returns following error message:

ERR_TOO_MANY_REDIRECTS

so.. I begin to test and try different things trying to understand what was happening.. and finally I found problem (but still did not find solution)

problem is that for some reason %{HTTPS} match to "off" if I use http it matches to off (correct) and if I use https:// it matches to "off" (and that is wrong! it should match to "on".

To be sure that it was the problem I replaced .htaccess content with following


    RewriteEngine On
    RewriteRule ^aaa.html$ https://%{HTTP_HOST}/foo.html?%{HTTPS} [R=301,L]

I did that to see value assumed by %{HTTPS}

And when I visit https://www.example.com/aaa.html I am redirected to

https://www.example.com/foo.hml?off when it should go to

https://www.example.com/foo.hml?on

why %{HTTPS} is off when I visit it using https?

5
  • "why %{HTTPS} is off when I visit it using https?" - could be that your hoster has some kind of proxy setup, so that your requests don't go to your web server immediately. What happens when you check %{SERVER_POST} instead?
    – C3roe
    Commented Jul 20, 2022 at 6:12
  • I tested "%{SERVER_POST}" it shows empty string...
    – CruzDelSur
    Commented Jul 20, 2022 at 14:22
  • Typo, I meant SERVER_PORT.
    – C3roe
    Commented Jul 21, 2022 at 6:31
  • @CBroe, %{SERVER_PORT} = 80, it is 80 still when using https
    – CruzDelSur
    Commented Jul 21, 2022 at 12:22
  • 2
    Then you should go and ask your server admin, about the specifics of the setup. My guess is that there is some sort of proxy before the web server, that simply passes the requests along via HTTP, even if the request to the proxy was made using HTTPS.
    – C3roe
    Commented Jul 21, 2022 at 12:37

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.