我一直在尝试构建一个像这样的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划分逻辑的另一种迭代,但是我不知道最终执行了最后一个规则中的情况。
-f rewriteCond的参数期望文件系统路径。
正确的语法是:
RewriteCond %{REQUEST_FILENAME} !^server/
RewriteCond /path/to/your/server/root/%{REQUEST_FILENAME} -f
RewriteRule ".*" "-" [L]
您可能也必须在第二个规则上编辑相同的指令。