I use the following .htaccess
code to enable friendly URLs in a website.
RewriteEngine on
RewriteCond %{REQUEST_FILENA开发者_如何学PythonME} !-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?
Adding this RewriteCond:
RewriteCond %{REQUEST_FILENAME} !\.png$
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.
精彩评论