在友好 URL 的上下文中处理损坏的图像
我使用以下 .htaccess
代码在网站中启用友好 URL。
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [L]
它按预期工作,但有一些东西让我烦恼。比方说,当我请求一个包含 的页面,并且
sth.png
不存在时服务器端,.htaccess
代码会指示服务器向index.php?sth.png
发出请求,这会导致整个框架完全不必要的负载网站。
我可以做什么来防止这种情况发生?
I use the following .htaccess
code to enable friendly URLs in a website.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [L]
It works as it's supposed to but there's something that bugs me. When, let's say, I request a page that has a <img src="sth.png" />
in it and sth.png
does not exist on the server, the .htaccess
code will instruct the server to make a request to index.php?sth.png
, which would result in a completely unnecessary load of the whole framework of the website.
What can I do to prevent that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
添加此 RewriteCond:
应将 PNG 文件排除在重写规则之外。
但正如评论中所说,我会考虑在 index.php 中处理 404,无论其类型如何 - 它们不应该经常发生,以至于加载 PHP 文件无论如何都会成为性能问题。
Adding this RewriteCond:
should exclude PNG files from being subjected to the Rewrite rules.
But as said in the comment, I would consider handling 404s within index.php regardless of their type - they shouldn't happen so often that loading the PHP file becomes a performance issue anyway.