mod_rewrite和.htaccess - 如果脚本/文件存在则停止重写

Tho*_*son 6 .htaccess mod-rewrite

这是我的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]
Run Code Online (Sandbox Code Playgroud)

现在,这会将所有内容转发到我的index.php脚本!如果脚本存在,它不会停止.任何人都知道为什么这不起作用?我到处都看,但我真的不懂mod_rewrite(就像我想的那样!).

问题出现了,因为我<script>在我的web目录中放入了指向.js文件的标签,然后转发到index.php脚本.Web开发人员工具栏告诉我这一点.:)在firefox的视图源窗口中单击指向js文件的链接也会显示index.php输出.

谢谢.

vbe*_*nce 8

这是因为在处理重写规则之后,整个过程将使用新的url重新启动.每次使用更改的URL,URL的处理可以一遍又一遍地通过规则,直到不再有更改(没有找到应用规则).

你需要这个:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*)         /index.php          [L]
Run Code Online (Sandbox Code Playgroud)

不要将规则视为一个程序,它们是可以重叠的规则,并且必须尽可能具体.