mod_rewrite 和 .htaccess - 如果脚本/文件存在则停止重写
这是我的 htaccess:
RewriteEngine On
# Stop if it's a request to an existing file.
RewriteCond %{REQUEST_FILENAME} -f [NC,OR]
RewriteCond %{REQUEST_FILENAME} -d [NC]
RewriteRule .* - [L]
# Redirect all requests to the index page
RewriteRule ^([^/]) /index.php [L]
现在这会将所有内容转发到我的 index.php 脚本!如果脚本存在,它不会停止。任何人都知道为什么这不起作用?我到处都看过,但我并不真正理解 mod_rewrite (正如我所想的那样!)。
问题的出现是因为我放入了 标记,这些标记指向我的 Web 目录中的 .js 文件,然后将其转发到 index.php 脚本。 Web 开发人员工具栏告诉我这一点。 :) 在 Firefox 的查看源窗口中单击 js 文件的链接也会显示 index.php 输出。
谢谢。
This is my htaccess:
RewriteEngine On
# Stop if it's a request to an existing file.
RewriteCond %{REQUEST_FILENAME} -f [NC,OR]
RewriteCond %{REQUEST_FILENAME} -d [NC]
RewriteRule .* - [L]
# Redirect all requests to the index page
RewriteRule ^([^/]) /index.php [L]
Now this forwards everything to my index.php script! It dosen't stop if a script exists. Anyone have any idea why this isn't working? I've looked everywhere, but I don't really understand mod_rewrite (as much as I thought!).
The problem has come about because I've put in <script>
tags which point to .js files in my web directory which are then forwarded to the index.php script. The web developer toolbar tells me this. :) And clicking links to the js files in firefox's view source window also shows the index.php output.
thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是因为在处理重写规则后,整个过程会使用新的 url 重新启动。每次更改 URL 时,URL 的处理都可以一遍又一遍地执行规则,直到不再发生更改(找不到应用规则)。
您需要这个:
不要将规则视为一个程序,它们是可以重叠的规则,并且每个规则必须尽可能具体。
This is because after processing a rewrite rule the whole process restarts with the new url. The processing of an url can go thru the rules over and over again each time with the changed URL, until there is no more change (no applying rules found).
You need this:
Don't think of the rules as a program, they are rules which can overlap and must be as specific as possible with each one.
我想你应该在第一个条件后添加 [OR],如下所示:
I guess you should add [OR] after the first condition like this: