htaccess重写

编程入门 行业动态 更新时间:2024-10-21 19:28:01
本文介绍了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"

我要重定向:

  • www.example
  • example
  • www.example
  • www.example
  • example
  • www.example

收件人:

  • example

我找到的最好的解释解决方案在这里: simonecarletti/blog/2016/08/redirect-domain-http-https-www-apache/

The best explained solution I've found is here: simonecarletti/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 ^ example%{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$ [NC] RewriteRule ^(.*)$ example/$1 [R=301,L]

...但是当然不会重定向example.

...But of course that doesn't redirect example.

我在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?

推荐答案

添加指定的RewriteCond和RewriteRule在使用或 时会给我?off_example.这是预期的吗?

Adding the RewriteCond and RewriteRule you specify gives me ?off_example when using or . Is that expected?

不,那不是预期的结果-这将是问题所在.通过...访问时,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 .... 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 ^ example%{REQUEST_URI} [L,NE,R=301]

另一种较不常见的方法是(前端)服务器设置HTTPS 环境变量(可能是由mod_ssl 提供??)-请注意,这与类似的HTTPS 服务器变量不同,例如上文提到的.此环境变量使用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 ^ example%{REQUEST_URI} [L,NE,R=301]

更多推荐

htaccess重写

本文发布于:2023-10-31 16:21:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1546621.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:重写   htaccess

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!