[英]Force non-www and HTTPS in htaccess redirect results in too many redirects
I'm trying to force non-www https in htaccess, but every example I find throws the error "too many redirects"我试图在 htaccess 中强制使用非 www https,但我发现的每个示例都会抛出错误“太多重定向”
I want to redirect:我想重定向:
http://www.example.com
http://example.com
https://www.example.com
to:到:
https://example.com
The best explained solution I've found is here:我找到的最好的解释解决方案是在这里:
https://simonecarletti.com/blog/2016/08/redirect-domain-http-https-www-apache/ 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]
...But of course that doesn't redirect http://example.com
. ...但是当然不会重定向http://example.com
。
I have no redirects setup in httpd.conf
, and None setup in Plesk.我在httpd.conf
中没有设置重定向,在 Plesk 中也没有设置。 I'm on CentOS 6.8, Apache 2.2.15, Plesk 12.5.我在 CentOS 6.8、Apache 2.2.15、Plesk 12.5 上。
What could be causing the issue?是什么导致了这个问题?
Adding the
RewriteCond
andRewriteRule
you specify gives me?off_example.com
when using http:// or https:// .使用 http:// 或https://时,添加您指定的RewriteCond
和RewriteRule
会给我?off_example.com
。 Is that expected?这是预期的吗?
No, that's not the expected result - that will be the problem.不,这不是预期的结果 - 这将是问题所在。 HTTPS
should be on
when accessed over https://...
.通过https://...
访问时应on
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.如果HTTPS
服务器变量从未设置为“on”(或者更确切地说,从未从“off”更改),那么您的RewriteRule
将导致重定向循环。
This suggests that the "Let's Encrypt addon in Plesk" is implemented via some kind of "proxy" (front-end) server?这表明“让我们在 Plesk 中加密插件”是通过某种“代理”(前端)服务器实现的? Your application server still responds over unencrypted HTTP to the proxy and the clients connection to the proxy is encrypted over HTTPS.您的应用程序服务器仍然通过未加密的 HTTP 响应代理,并且客户端与代理的连接通过 HTTPS 加密。 At least, that's what it looks like - but your host should be able to confirm this.至少,它看起来是这样的——但你的主人应该能够确认这一点。
If this is the case then the proxy usually sets additional HTTP request headers with details about the client's connection.如果是这种情况,那么代理通常会设置额外的 HTTP 请求标头,其中包含有关客户端连接的详细信息。 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").您应该能够检查您的应用程序看到的请求标头,但X-Forwarded-Proto
标头通常设置为正在使用的协议(“http”或“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]
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.另一种不太常见的方法是让(前端)服务器设置一个HTTPS
环境变量(可能由 mod_ssl 提供?)——请注意,这与上面提到的名称相似的HTTPS
服务器变量不同。 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 .使用 mod_rewrite 指令中的语法%{ENV:HTTPS}
访问此环境变量,如果设置,则包含与HTTPS
服务器变量提供的值相同的值。 For example:例如:
RewriteEngine On
RewriteCond %{ENV:HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^ https://example.com%{REQUEST_URI} [L,NE,R=301]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.