.htaccess 中的重写规则:它有什么作用

发布于 2024-09-24 02:35:03 字数 403 浏览 2 评论 0原文

.htaccess 中的这一行有什么作用?我什么时候需要它?

RewriteRule !.(js|css|ico|gif|jpg|png)$ index.php

我正在使用 zend,我注意到 zend 生成的 .htaccess 没有这条规则,但我看过一个教程,其中有它但没有解释原因。

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

What does this line in .htaccess do, and when would I need it?

RewriteRule !.(js|css|ico|gif|jpg|png)$ index.php

I'm working with zend and I noticed the .htaccess generated by zend doesn't have this rule, but I've seen a tutorial that has it without explaining why.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

凉城凉梦凉人心 2024-10-01 02:35:03

这是一个 RewriteRule,将除这些文件类型(js、CSS、图标、GIF/JPG/PNG 图形)的请求之外的每个请求重定向到 index.php

这样一来,对静态资源的请求就不会由 index.php 处理(这通常是一件好事,因为启动 PHP 实例的成本很高。)

但是,它已经在下面的代码块中进行了处理。这部分:

RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]

排除物理存在的文件 (-s) 到 index.php 的重定向;符号链接(-);和现有目录(-d)。

如果您使用引用的第二个块(由 Zend 创建的块),一切都很好。

Apache 手册中的参考

It's a RewriteRule redirecting every request except requests for those file types (js, CSS, icons, GIF/JPG/PNG graphics) to index.php.

It's so that requests for static resources don't get handled by index.php (which is generally a good thing, because starting a PHP instance is expensive.)

It is however already handled in the code block below. This part:

RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]

Excludes the redirection to index.php for files that physically exist ( -s); symbolic links ( -); and existing directories (-d).

If you use the second block you quote (the one created by Zend), everything's fine.

Reference in the Apache manual

青朷 2024-10-01 02:35:03

规则:

RewriteRule !.(js|css|ico|gif|jpg|png)$ index.php

基本上是说:“如果请求不(!)以 .js/css/ico/giv/jpg/png 结尾($),则将其重定向到index.php”。

您需要此规则来避免图像/样式表和 js 脚本请求传递到 index.php。

The rule:

RewriteRule !.(js|css|ico|gif|jpg|png)$ index.php

basically says: "If the request does NOT (!) end ($) with .js/css/ico/giv/jpg/png redirect it to index.php".

You'd need this rule to avoid images/stylesheets and js script requests being passed to index.php.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文