尽管前一个

时间:2017-03-13 10:25:33

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

我一直在尝试构建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重写逻辑的另一次迭代,但我不知道这会导致最后一条规则最终被执行。

1 个答案:

答案 0 :(得分:0)

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

正确的语法是:

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

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