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

发布于 2024-10-22 21:30:50 字数 613 浏览 2 评论 0原文

这是我的 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 (正如我所想的那样!)。

问题的出现是因为我放入了

谢谢。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

相守太难 2024-10-29 21:30:50

这是因为在处理重写规则后,整个过程会使用新的 url 重新启动。每次更改 URL 时,URL 的处理都可以一遍又一遍地执行规则,直到不再发生更改(找不到应用规则)。

您需要这个:

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

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

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:

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

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.

夏末染殇 2024-10-29 21:30:50

我想你应该在第一个条件后添加 [OR],如下所示:

RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule (.*) $1 [QSA,L]

I guess you should add [OR] after the first condition like this:

RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule (.*) $1 [QSA,L]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文