【问题标题】:Does %{REQUEST_URI} always begin with slash?%{REQUEST_URI} 是否总是以斜杠开头?
【发布时间】:2016-05-21 03:21:46
【问题描述】:

我已经在 2-3 台 apache 服务器中进行了测试,如果我没有出卖我的想法,%{REQUEST_URI} 总是以 / 开头,即使请求的 URL 是 http://domain.com(没有和结尾的斜线) )。但是我已经看到 .htaccess 文件有这样的规则:

RewriteRule ^[^/]*$ http...

我的意思是,这种规则会匹配{REQUEST_URI}吗?或者我可能遗漏了一些关于 mod_rewrite 的内容(RewriteRule 之后的参数不与%{REQUEST_URI} 相同,因此不会发生匹配吗?)。

如果有人能告诉我这件事,将不胜感激。

【问题讨论】:

标签: regex apache .htaccess mod-rewrite url-rewriting


【解决方案1】:

RewriteRule 模式不一定与%{REQUEST_URI} 匹配。来自the docs

匹配的是什么?

VirtualHost 上下文中,Pattern 将首先匹配 在主机名和端口之后和之前的 URL 部分 查询字符串(例如“/app1/index.html”)。

Directory 和 htaccess 上下文中,Pattern 最初将是 匹配 filesystem 路径,删除前缀后 将服务器引导至当前的RewriteRule(例如“app1/index.html”或 “index.html”取决于指令的定义位置)。

由于您在htaccess 文件中看到了这些,因此要匹配的路径不会以/ 开头,因为删除的前缀将至少包含/

【讨论】:

    【解决方案2】:

    .htaccess 是每个目录指令,Apache 从 RewriteRule URI 模式中去除当前目录路径(因此如果放置在 DocumentRoot 中,则以斜杠开头)。

    另一方面,%{REQUEST_URI} 总是包含从域名后的/ 开始的完整 URI 值。

    这个 URL 的这些例子会很清楚:

    URL: http://domain.com/path/files/user.php
    

    案例1: .htaccess 放在DocumentRoot

    %{REQUEST_URI}: /path/files/user.php
    RewriteRule pattern: path/files/user.php
    

    案例2: .htaccess 放在DocumentRoot/path/

    %{REQUEST_URI}: /path/files/user.php
    RewriteRule pattern: files/user.php
    

    案例3: .htaccess 放在DocumentRoot/path/files/

    %{REQUEST_URI}: /path/files/user.php
    RewriteRule pattern: user.php
    

    【讨论】:

      猜你喜欢
      • 2015-02-03
      • 1970-01-01
      • 1970-01-01
      • 2016-09-28
      • 1970-01-01
      • 2013-11-14
      • 2011-11-08
      • 2012-05-26
      • 1970-01-01
      相关资源
      最近更新 更多