问题描述
我正在尝试在htaccess中强制使用非www https,但是我发现的每个示例均会引发错误重定向过多"
I'm trying to force non-www https in htaccess, but every example I find throws the error "too many redirects"
我要重定向:
-
http://www.example.com
-
http://example.com
-
https://www.example.com
收件人:
-
https://example.com
我找到的最好的解释解决方案在这里:
https://simonecarletti.com/blog/2016/08/redirect-domain-http-https-www-apache/
The best explained solution I've found is here:
https://simonecarletti.com/blog/2016/08/redirect-domain-http-https-www-apache/
...下面给出了以下示例:
...Which gives the following example:
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^ https://example.com%{REQUEST_URI} [L,NE,R=301]
通过解释,我了解它在做什么,但是我仍然遇到相同的错误-重定向过多.
Thanks to the explanation I understand what it's doing, but I'm still getting the same error - too many redirects.
我能做的最好的事情是:
The best I have been able to do is:
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]
...但是,当然不会重定向 http://example.com
.
...But of course that doesn't redirect http://example.com
.
我在 httpd.conf
中没有重定向设置,在Plesk中没有None设置.我使用的是CentOS 6.8,Apache 2.2.15,Plesk 12.5.
I have no redirects setup in httpd.conf
, and None setup in Plesk. I'm on CentOS 6.8, Apache 2.2.15, Plesk 12.5.
可能是什么原因引起的?
What could be causing the issue?
推荐答案
不,那不是预期的结果-这将是问题所在.通过 https://...
访问时, HTTPS
应该在 on
上.如果 HTTPS
服务器变量从未设置为"on"(或者永远不从"off"更改),则您的 RewriteRule
将导致重定向循环.
No, that's not the expected result - that will be the problem. HTTPS
should be on
when accessed over https://...
. If the HTTPS
server variable is never set to "on" (or rather, never changes from "off") then your RewriteRule
will result in a redirect loop.
这表明"Plesk中的我们加密附件"是通过某种代理"(前端)服务器实现的吗?您的应用程序服务器仍然通过未加密的HTTP响应代理,并且与代理的客户端连接已通过HTTPS加密.至少看起来是这样-但您的房东应该能够确认这一点.
This suggests that the "Let's Encrypt addon in Plesk" is implemented via some kind of "proxy" (front-end) server? Your application server still responds over unencrypted HTTP to the proxy and the clients connection to the proxy is encrypted over HTTPS. At least, that's what it looks like - but your host should be able to confirm this.
如果是这种情况,则代理通常会设置其他HTTP请求标头,其中包含有关客户端连接的详细信息.您应该能够检查应用程序看到的请求标头,但是通常使用正在使用的协议("http"或"https")来设置 X-Forwarded-Proto
标头").如果是这种情况,那么您可能可以将现有指令更改为如下所示的内容:
If this is the case then the proxy usually sets additional HTTP request headers with details about the client's connection. You should be able to examine the request headers that your application sees, but it is common for the X-Forwarded-Proto
header to be set with the protocol that is being used ("http" or "https"). If this is the case then you can probably change your existing directives to something like the following instead:
RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} !https [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^ https://example.com%{REQUEST_URI} [L,NE,R=301]
另一种不太常见的方法是(前端)服务器设置 HTTPS
环境变量(可能是由mod_ssl提供?)-请注意,这与类似的 HTTPS
服务器变量.此 environment变量使用mod_rewrite指令中的语法%{ENV:HTTPS}
进行访问,并且,如果设置了该变量,则包含与相同的值.> HTTPS
服务器变量.例如:
Another, less common method is for the (front-end) server to set an HTTPS
environment variable (possibly provided by mod_ssl?) instead - note that this is different to the similarly named HTTPS
server variable, as mentioned above. This environment variable is accessed using the syntax %{ENV:HTTPS}
in the mod_rewrite directive and, if set, contains the same value as would otherwise be provided by the HTTPS
server variable. For example:
RewriteEngine On
RewriteCond %{ENV:HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^ https://example.com%{REQUEST_URI} [L,NE,R=301]
这篇关于强制htaccess重定向中的非www和HTTPS导致太多重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!