htaccess重写-重定向过多 [英] htaccess rewrite - too many redirects

查看:62
本文介绍了htaccess重写-重定向过多的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在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
  • 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?

推荐答案

添加指定的RewriteCondRewriteRule在使用http://或 https://时会给我?off_example.com.这是预期的吗?

Adding the RewriteCond and RewriteRule you specify gives me ?off_example.com when using http:// or https://. Is that expected?

不,那不是预期的结果-这将是问题所在.通过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请求标头,其中包含有关客户端连接的详细信息.您应该能够检查您的应用程序看到的请求标头,但是X-Forwarded-Proto标头通常使用所使用的协议("http"或"https")进行设置.如果是这种情况,那么您可能可以将现有指令更改为如下所示的内容:

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 环境变量(可能是


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重写-重定向过多的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆