%{REQUEST

编程入门 行业动态 更新时间:2024-10-10 02:23:35
本文介绍了%{REQUEST_URI} 总是以斜杠开头吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我已经在 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

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

发布评论

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

>www.elefans.com

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