php - 在友好 URL 的上下文中处理损坏的图像

标签 php apache .htaccess mod-rewrite friendly-url

我使用以下.htaccess在网站中启用友好 URL 的代码。

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [L]

它按预期工作,但有一些东西让我烦恼。比方说,当我请求一个具有 <img src="sth.png" /> 的页面时在其中和 sth.png服务器上不存在 .htaccess代码将指示服务器向 index.php?sth.png 发出请求,这将导致网站整个框架完全不必要的负载。

我可以做什么来防止这种情况发生?

最佳答案

添加此 RewriteCond:

RewriteCond %{REQUEST_FILENAME} !\.png$

应将 PNG 文件排除在重写规则之外。

但正如评论中所说,我会考虑在 index.php 中处理 404,无论其类型如何 - 它们不应该经常发生,以至于加载 PHP 文件无论如何都会成为性能问题。

关于php - 在友好 URL 的上下文中处理损坏的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7703166/

相关文章:

php - “无法设置私钥文件”试图使用 php 发送推送

php - 锁定表如何工作?

php - 在 Magento 中没有收到成功消息

windows - XAMPP - 端口 80 被 PID 为 4 的 "Unable to open process"使用! 12

apache - 如何将我的 Apache .htaccess 重写规则转换为 lighttpd

php - PHP 中的 self::vs className::inside static className 方法

java - 复制带空格的文件

java - Apache-cxf JAXRSClient 自定义日期格式

apache - htaccess 将 www (es) 重定向到非 www (es)

php - SSL 错误 : how to redirect https://www. domain.com 通过 .htaccess 到 http ://domain. com 以避免 SSL 错误