.htaccess Magento网站返回大404页的src元素中丢失的图像

hjqgdpho  于 2022-11-16  发布在  其他
关注(0)|答案(1)|浏览(103)

.htaccess中,我们有:

RewriteEngine on

############################################
## you can put here your magento root folder
## path relative to web root

        RewriteBase /

############################################
## workaround for HTTP authorization
## in CGI environment

    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

############################################
## always send 404 on missing files in these folders

    RewriteCond %{REQUEST_URI} !^/(media|skin|js)/

############################################
## never rewrite for existing files, directories and links

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-l

############################################
## rewrite everything else to index.php

    RewriteRule .* index.php [L]

如果我导航到一个不存在的图片,通过把这个网址放在我的浏览器的地址栏,例如www.example.com/media/catalog/non_existent_image.jpg,那么我会得到一个完整的404页面作为我的网站主题的一部分。这是有意义的,因为它给了用户机会点击主页按钮,或使用网站的菜单,同时仍然被通知他们正在寻找的图片不存在。
但我发现每次页面上出现<img src='www.example.com/media/catalog/non_existent_image.jpg'>标签时,都会返回相同的404页面。这意味着每丢失一张图片都会导致整个Magento应用程序的一个新示例被启动,只是为了向浏览器发送一个404页面,这对改善用户体验毫无帮助。事实上,这明显减慢了页面加载速度,增加了服务器的负载。
当用户试图使用地址栏导航到丢失的图像时,如何将一个简单的HTML 404发送回浏览器以查找丢失的资源,同时仍然保持完整的用户体验?
我认为这应该在.htaccess中解决,而不是在Magento中做任何事情。我认为,在Magento中做这件事,需要在很早的时候,在index.php文件中短路代码,以防止它做很多无意义的工作。

uidvcgyl

uidvcgyl1#

您需要检查资源请求的引用者,以确定是否应通过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]

我已经用REST客户端测试过了。当请求一个不存在的图像并将HTTP_REFERER留空时,我得到的是index.php内容(在我的例子中,是一个字符串“Foobar”)。如果我将referer设置为http://example.com/test,我得到的是Apache 404(“Object Not Found”)。
我在这里包含了RewriteEngine指令,暗示这些应该是在做其他事情之前进行的第一次检查。

相关问题