我已经在 2-3 个 apache 服务器上进行了测试,如果我不介意的话,%{REQUEST_URI} 总是以 / 开头,即使如果请求的 URL 是 domain(不带斜杠和结尾斜杠).但是我已经看到 .htaccess 文件有这样的规则:
RewriteRule ^[^/]*$ http...我的意思是,这种规则会匹配 {REQUEST_URI} 吗?或者我可能遗漏了一些关于 mod_rewrite 的东西(RewriteRule 之后的参数是不是与 %{REQUEST_URI} 相同,因此不会发生匹配?).
如果有人能就此启发我,我将不胜感激.
解决方案RewriteRule 模式不一定与 %{REQUEST_URI} 匹配.来自文档:
匹配的是什么?在 VirtualHost 上下文中,最初将匹配模式针对 URL 中主机名和端口之后和之前的部分查询字符串(例如/app1/index.html").
在 目录 和 htaccess 上下文中,模式最初将是与 filesystem 路径匹配,删除前缀后将服务器引导至当前的 RewriteRule(例如app1/index.html"或index.html"取决于指令的定义位置).
因为您在 htaccess 文件中看到这些,所以要匹配的路径不会以 / 开头,因为删除的前缀将至少包含一个 /.
I've performed tests in 2-3 apache servers and, if my mind doesn't betray me, %{REQUEST_URI} always starts with an /, even if the requested URL is domain (without and ending slash). However i've seen .htaccess files that have rules like this one:
RewriteRule ^[^/]*$ http...I mean, would this kind of rule ever match {REQUEST_URI}? Or maybe i'm missing something about mod_rewrite (isn't the parameter after RewriteRule just the same as %{REQUEST_URI} and therefore no match would happen?).
If someone can enlighten me about this, it would be very appreciated.
解决方案RewriteRule patterns do not necessarily match against %{REQUEST_URI}. From the docs:
What is matched?In VirtualHost context, The Pattern will initially be matched against the part of the URL after the hostname and port, and before the query string (e.g. "/app1/index.html").
In Directory and htaccess context, the Pattern will initially be matched against the filesystem path, after removing the prefix that led the server to the current RewriteRule (e.g. "app1/index.html" or "index.html" depending on where the directives are defined).
Since you saw these in an htaccess file, the path to be matched will not start with a /, since the prefix removed would contain at least a /.
更多推荐
%{REQUEST
发布评论