apache - Magento site returns large 404 page for missing images in src elements -
in .htaccess
, have this:
rewriteengine on ############################################ ## can put here magento root folder ## path relative web root rewritebase / ############################################ ## workaround http authorization ## in cgi environment rewriterule .* - [e=http_authorization:%{http:authorization}] ############################################ ## send 404 on missing files in these folders rewritecond %{request_uri} !^/(media|skin|js)/ ############################################ ## never rewrite existing files, directories , links rewritecond %{request_filename} !-f rewritecond %{request_filename} !-d rewritecond %{request_filename} !-l ############################################ ## rewrite else index.php rewriterule .* index.php [l]
if navigate non existent image on site, putting url browser's address bar, example www.example.com/media/catalog/non_existent_image.jpg
, nice full 404 page part of site's theme. makes sense, because gives user opportunity click home button, or use site's menu, while still being notified image looking doesn't exist.
but i've found out same 404 page being returned every time <img src='www.example.com/media/catalog/non_existent_image.jpg'>
tag appears on page. means every missing image causes new instance of entire magento app fired send browser 404 nothing improve user experience. in fact noticeably slows down page loading , adds load server.
how can send simple html 404 browser missing resources, while still keeping full user experience when user tries navigate missing image address bar?
i'm thinking should solvable in .htaccess
, rather doing in magento. doing in magento would, think, require short-circuiting code early, in index.php
file, prevent doing lot of pointless work.
you need check referrer of resource request in order determine whether or not should routed through index.php
:
rewriteengine on rewritecond %{http_referer} ^http\:\/\/example\.com\/ rewritecond %{request_filename} !-f rewriterule ^(?:media|skin|js)/.+ - [l] rewritecond %{http_referer} !^http\:\/\/example\.com\/ rewritecond %{request_filename} !-f rewriterule ^(?:media|skin|js)/.+ index.php [l]
i've tested rest client. when requesting non-existent image , leaving http_referer
blank, served index.php
content (in case, string says "foobar"). if set referrer http://example.com/test
, served apache 404 ("object not found").
i've included rewriteengine
directive here imply these should first checks made, before else done.
Comments
Post a Comment