%{REQUEST_URI} 总是以斜杠开头吗? [英] Does %{REQUEST_URI} always begin with slash?

查看:59
本文介绍了%{REQUEST_URI} 总是以斜杠开头吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在 2-3 个 apache 服务器上进行了测试,如果我不介意的话,%{REQUEST_URI} 总是以 / 开头,即使如果请求的 URL 是 http://domain.com(不带斜杠和结尾斜杠).但是我已经看到 .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 http://domain.com (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_URI} 总是以斜杠开头吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆