【问题标题】:RewriteRule statement is evaluated despite [L] flag on a prior one尽管前面有 [L] 标志,但仍评估 RewriteRule 语句
【发布时间】:2017-03-13 10:25:33
【问题描述】:

我一直在尝试构建一个 if/else 分支逻辑,如下所示:

if ( <URL> corresponds to an existing file ( with the exception of those within the "server" directory ) ) {
    don't modify <URL>
}
else if ( "pages/<URL>.(html|php)" corresponds to an existing file ) {
    re-write it to that
}
else {
    re-write the URL to index.php
}

这是我想出的.htaccess 文件:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !^server/
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ".*" "-" [L]

RewriteCond "pages/%{REQUEST_FILENAME}\.(html|php)" -f
RewriteRule "(.*)" "pages/%1.$1" [L]

RewriteRule ".*" "index.php" [L]

我遇到的问题是这样的:

当我访问server/lib/router.php 时,它会执行该脚本,而它应该执行index.php

现在,我知道.htaccess 中的 L 标志触发了 URL 重写逻辑的另一次迭代,但我不明白这如何导致最后一个规则最终被执行。

【问题讨论】:

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


    【解决方案1】:

    RewriteCond 的-f 参数需要一个文件系统路径。

    正确的语法是:

    RewriteCond %{REQUEST_FILENAME} !^server/
    RewriteCond /path/to/your/server/root/%{REQUEST_FILENAME} -f
    RewriteRule ".*" "-" [L]
    

    您可能还必须在第二条规则上编辑相同的指令。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-01-04
      • 2020-08-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-06
      • 1970-01-01
      相关资源
      最近更新 更多