在友好的url上下文中处理破碎的图像


Dealing with broken images in the context of friendly URLs

我使用以下.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发出请求,这将导致整个网站框架的完全不必要的负载。

我能做些什么来防止呢?

添加rewritecsecond:

RewriteCond %{REQUEST_FILENAME} !'.png$

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

但正如评论中所说,我会考虑在index.php中处理404,而不管它们的类型是什么——它们不应该经常发生,否则加载PHP文件就会成为性能问题。