mod-rewrite THE_REQUEST 和(REQUEST_URI 或 REQUEST_FILE)给出不同的文件名 - 为什么?
我正在尝试 mod-rewrite。我以为我想要的很简单,但我没有通过 REQUEST_URI 获取请求文件的 URI。相反,传递的名称被传递。
手册说:
THE_REQUEST
完整的 HTTP 请求 浏览器发送到服务器的行 (例如,“GET /index.html HTTP/1.1”)。 这不包括任何额外的 浏览器发送的标头。这 价值并没有被逃脱 (解码),与大多数其他变量不同 如下。
REQUEST_URI
请求的资源 在 HTTP 请求行中。 (在 上面的例子,这将是 “/index.html”。)
但是,这两个在我的测试中给出了不同的文件名。我有一个 bootstrap.php,我想通过它发送所有请求。这是测试文件:
<?php
echo $_GET['requestedURI'];
?>
在 .htaccess 文件上,我有:
### REWRITE RULES ###
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .+ bootstrap.php?requestedURI=%{REQUEST_URI} [L]
请求 http://localhost/test.htm
输出: /bootstrap.php
如果我改为放置 THE_REQUEST .htaccess 中的 REQUEST_URI 我得到 GET /test.htm HTTP/1.1
那么为什么不满足于 THE_REQUEST 呢?好吧,一旦查询字符串存在,事情就会崩溃。如果我请求: http://localhost/test.htm?x=1&y=2
我得到 GET /test.htm?x=1 第一个&符号中断事物。我认为应该可以用 %26 替换查询字符串中的所有 & 符号,这样它就可以工作,但到目前为止我还没有成功...
所以任何人都可以告诉为什么 REQUEST_URI 失败以及如何修复它或如何将 & 符号重写为查询字符串中的%26?
谢谢。
编辑:上述报告适用于 Win 7 上的 xampp 1.7.3。同时我在生产 Linux 系统上尝试了它,并且 REQUEST_URI 返回了它应该返回的内容。
I was experimenting with mod-rewrite. I thought what I wanted was simple but I don't get the requested file's URI via REQUEST_URI. Rather the delived name is passed on.
The manual says:
THE_REQUEST
The full HTTP request
line sent by the browser to the server
(e.g., "GET /index.html HTTP/1.1").
This does not include any additional
headers sent by the browser. This
value has not been unescaped
(decoded), unlike most other variables
below.REQUEST_URI
The resource requested
in the HTTP request line. (In the
example above, this would be
"/index.html".)
However the two give different file names in my tests. I have a bootstrap.php through which I wanted to send all requests. This is the test file:
<?php
echo $_GET['requestedURI'];
?>
on the .htaccess file I have:
### REWRITE RULES ###
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .+ bootstrap.php?requestedURI=%{REQUEST_URI} [L]
Requesting http://localhost/test.htm
puts out: /bootstrap.php
if I put THE_REQUEST instead of REQUEST_URI in the .htaccess I get GET /test.htm HTTP/1.1
So why not settle for THE_REQUEST? Well, as soon as a query string exists things break. If I request: http://localhost/test.htm?x=1&y=2
I get GET /test.htm?x=1 the first ampersand breaks things. I think it should be possible to replace all ampersands in the querystring with %26 so that it would work but I did not manage so far...
So can anyone tell why REQUEST_URI fails and how to fix it or how to rewrite the ampersands to %26 in the query string?
Thanks.
EDIT: The above report applies to xampp 1.7.3 on Win 7. I tried it on a production Linux system in the meantime and there REQUEST_URI returns what it should.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您无需显式传递请求的 URI 路径和查询,因为您可以通过
$_SERVER['REQUEST_URI']
在 PHP 中访问它。因此这应该足够了:You don’t need to explicitly pass the requested URI path and query as you can access it in PHP via
$_SERVER['REQUEST_URI']
. Thus this should suffice:我环顾四周,没有找到任何好的解释为什么
%{REQUEST_URI}
的行为与您的示例中的一样。实现您所追求的目标的最常见方法似乎是反向引用:编辑
根据您的评论,
REQUEST_URI
和REQUEST_FILENAME
似乎是当重写规则被触发时更新并重新评估。I have looked around a bit, and haven't found any good explanation why
%{REQUEST_URI}
behaves like it does in your example. The most common way to achieve what your're after seems to be backreferences:Edit
Based on your comment, it seems like
REQUEST_URI
andREQUEST_FILENAME
are updated and re-evaluated when the rewriterule is triggered.