了解旧的 .htaccess 文件

Understanding an old .htaccess file

有人可以向我解释一下这个 RewriteCond 吗? 特别是末尾的 HTTP/ 部分:

RewriteEngine on
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
RewriteRule ^index\.php$ http://www.XXXXXXXXXXXXXXXX.com/ [R=301,L] 

我已经找到了对条件的前半部分的很好解释here,但我仍然对“/index.php\HTTP/”部分感到困惑。

再说一次:为什么是“HTTP/”?

I found a very good explanation for the first half of the condition already here, but I'm still confused with the "/index.php\ HTTP/" portion.

该解释省略了一些细节。 THE_REQUEST 服务器变量包含 HTTP 请求的第一行 headers。并将包含以下形式的字符串:

GET /index.php HTTP/1.1

所以,它包含3条信息,用spaces分隔。您有请求方法(GET、POST、OPTIONS、HEAD 等)、URI 和使用的 protocol/version(本例中为 HTTP v1.1)。

对于这个特定的正则表达式,您不一定需要包括最后的 HTTP/ 部分,只需要包括 space。但是,通过包含 http/ 可以使正则表达式更易于阅读并且不易出错。以 space(您看不到)结束并不是最佳做法。

以下是等价的:

RewriteCond %{THE_REQUEST} \s/index\.php\s

这使用 shorthand 字符 class 表示白色 space (\s) 而不是转义文字 space(所以你可以“看到”space)。

或者,使用双引号将参数括起来:

RewriteCond %{THE_REQUEST} " /index\.php "

(虽然,乍一看这几乎像是一个错误。)

首先包含此 条件RewriteCond 指令)的原因是为了防止重写循环,如果您将请求重写为 index.php 之后(front-controller 模式)。 THE_REQUEST 在 Apache 内部重写时不会在整个请求中更新(与其他变量不同)。