问题描述
多年来我一直在为我的 CMS 使用简单的 mod_rewrite 规则,现在正在制作一个新版本,我发现 rewriteCond 没有意义 - 我有标准的if is not a file",但我仍然看到 rewriterules即使他们不应该进行评估.我的重写代码:
I have been using simple mod_rewrite rules for my CMS for years and am now making a new version, I am seeing the rewriteCond not making sense- I have the standard "if is not a file" but I still see the rewriterules being evaluated even though they're not supposed to. My rewrite code:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ index.php?page=$1
RewriteRule ^([^/]+)/([^/]+)$ index.php?page=$1&var=$2
我加载/page/var 并且它作为 index.php?page=page&var=var 工作正常,但我尝试加载/css/file.css 并且它加载 index.php?page=css&var=file.css 即使/css/file.css 是一个文件,所以整个重写部分甚至不应该被评估.
I load /page/var and it works fine as index.php?page=page&var=var, but I try to load /css/file.css and it loads index.php?page=css&var=file.css even though /css/file.css is a file, so the whole rewrite section shouldn't even be evaluated.
我从未见过 htaccess 明显违背其自身的逻辑,有人可以帮我解决这个问题吗?有没有人遇到过这样的事情?
I've never seen htaccess apparently defy its own logic, can someone help me figure this out? Has anyone ever run across anything like this?
推荐答案
RewriteCondition
仅适用于下一条规则.你想要这个:
The RewriteCondition
s only apply to the next rule. You want this:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ index.php?page=$1
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)$ index.php?page=$1&var=$2
这篇关于.htaccess !-f 规则不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!