0

我们有几种不同尺寸的产品图片。每当缺少产品图像时,我们都希望返回正确尺寸的默认图像。

每个产品的图像路径都不同。path /www/docroot/images/brand/product/product1_l.jpg path /www/docroot/images/differentbrand/differentproduct/someotherproduct1_l.jpg 图像的大小看起来像左边的项目。我们希望在右侧选择相应的默认图像。

product1_l.jpg large    noimage_l.jpg
product1_m.jpg medium   noimage_m.jpg
product1_s.jpg small    noimage_s.jpg

我已经看到一些使用 mod_rewrite 实现类似目标的示例。任何人都可以就我如何使用它来适应我的场景提供任何指导吗?

4

2 回答 2

0

You need an intelligent 404 handler. If Apache throws a 404 (File not found) error the intelligent handler should try to salvage the event by looking at the pattern of the URL, and locating a known resource to serve instead. You should also handle the possibility that the fall back image might also not be there.

This page can show you how to check if a file is present, but the logic will need to be in the last line.

http://www.phpriot.com/articles/search-engine-urls/5

RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1

Or you can go the .htaccess route (I think that's cleaner)

http://httpd.apache.org/docs/2.2/mod/core.html#errordocument

Assuming you could use .htaccess and PHP here is one solution:

Add this line to your .htaccess:

ErrorDocument 404 /error.php

Then add this code to you error.php like this

<?php
 if(preg_match("/^/images/thumbnail/", $_SERVER['REQUEST_URI'])) {
   //do a 302 redirect
   header( 'Location: /images/defualtImage.jpg' ) ;
 } else {
   //You need to go somewhere to handle actual 404 errors
   header( 'Location: /static/404.html' ) ;
 }
?>

302 redirect directs would work best here.

于 2012-07-27T00:22:57.260 回答
0

谢谢你们的评论。我们最终采用了不同的解决方案。我们使用 onerror js 标记指向适当的 noimg 位置。这对我们的环境来说更清洁、更容易

于 2012-08-07T17:34:48.257 回答