Redirect loop when forcing www. hostname and SSL

时间:2015-06-15 14:40:23

标签: apache .htaccess redirect ssl

I'm going crazy – I just checked and rechecked my rules but I still don't understand why the server is producing a redirect loop?

The goal is to force as well www. as SSL.

RewriteCond %{HTTP_HOST} ^example\.com$ [NC,OR]
RewriteCond %{HTTPS} off
RewriteRule (.*) https://www.example.com/$1 [R=301,L]

The redirect works fine for incorrect URLs (either without www. or without SSL), but for correct ones I end up with a infinite redirect loop: https://www.example.com/uiae -> https://www.example.com/uiae

Is it possible that the server cannot handle the HTTPS-variable?

The Apache Documentation states that that variable would be safe to use for Apache 2.2.

I also tried SERVER_PROTOCOL !=https with the same result.

Without my rules I can access as well HTTP as HTTPS URLs, which shows that there is no other rules outside of my .htaccess.

Thanks for your help.

1 个答案:

答案 0 :(得分:0)

事实证明,托管服务提供商(在这种情况下是Strato.de)在该变量的支持下搞砸了。

他们的陈述是

  

Strato不支持此变量

这是完全出乎意料的,因为它是标准的Apache变量,正如文档所述。

解决方法是改为使用协议:

RewriteCond %{HTTP_HOST} ^example\.com$ [NC,OR]
RewriteCond %{SERVER_PORT} !443
RewriteRule (.*) https://www.example.com/$1 [R=301,L]