mod_rewrite 在每个重写规则之后评估 {REQUEST_FILENAME} 吗?
情况如下:
RewriteEngine On
RewriteBase /app/webroot/
RewriteRule ^(.*)$ $1 [QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
第二条规则的 RewriteConds 是否使用第一个 RewriteRule 的结果?
例如,如果我请求 /onestaticfile.txt
并且该文件实际上存在于 /app/webroot 中,如何确保最终网址为 /app/webroot/onestaticfile.txt< /code> 而不是
/app/webroot/index.php?url=onestaticfile.txt
?
Here is the case :
RewriteEngine On
RewriteBase /app/webroot/
RewriteRule ^(.*)$ $1 [QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
Does the RewriteConds for the second rule use the result of the first RewriteRule ?
For instance, if i request /onestaticfile.txt
and that file actually exists in /app/webroot, how to be sure that the final url would be /app/webroot/onestaticfile.txt
instead of /app/webroot/index.php?url=onestaticfile.txt
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将评估第二条规则的 RewriteConds。但由于
onestaticfile.txt
存在,它将无法通过 !-f 测试,并且不会应用第二条规则(因此最终网址将是:/app/webroot/onestaticfile.txt< /代码>)。同样,如果
onestaticfile.txt
不存在,则将应用第二条规则。无论哪种情况,当应用规则时,都会发生内部重定向,并且整个事情会随着重写的 URI 再次发生。但第二次不会更改基本 URI(没有查询字符串),因此第二次不会发生任何事情。
如果第一条规则的方括号中有一个 L,则永远不会应用第二条规则,因为重写始终会在第一条规则处结束。
The RewriteConds for the second rule will get evaluated. But since
onestaticfile.txt
exists, it will fail the !-f test and the second rule won't be applied (so final url will be:/app/webroot/onestaticfile.txt
). Likewise, ifonestaticfile.txt
doesn't exist, the 2nd rule would get applied.In either case, when the rule is applied, an internal redirect happens and the whole thing happens again with the rewritten URI. But the 2nd time around doesn't change the base URI (without the query strings) so nothing happens the second time around.
If you had an L in the square brackets of your first rule, the 2nd rule would never be applied because rewriting will always end on the first Rule.