.htaccess for missing images with wildcards

Viewed 90

I have a folder full of product images. And the image file name is something akin to:

/images/product_15_small.jpg
/images/product_15_large.jpg
/images/product_201_small.jpg
/images/product_201_large.jpg
/images/product_47502_small.jpg
/images/product_47502_large.jpg

If there is an error in the upload process and one of the files doesn't get created, say /images/product_201_large.jpg, I'd like Apache to replace that file with this file: /images/missing_0_large.jpg.

And in the case of the smaller image, I want it to be replaced with: /images/missing_0_small.jpg.

I'm not sure how to do conditional RedirectMatch using HT access and a wildcard. Could someone guide me? Here are the rules that I've tried to experiment with but none of them worked:

#TEST 1:
RedirectMatch 404 ^images/product_(\d+)_small.jpg   images/missing_(\d+)_small.jpg
RedirectMatch 404 ^images/product_(\d+)_large.jpg   images/missing_(\d+)_large.jpg

#TEST 2:
RewriteRule ^images/product_(\d+)_small.jpg     images/missing_(\d+)_small.jpg [L]
RewriteRule ^images/product_(\d+)_large.jpg     images/missing_(\d+)_large.jpg [L]

From the example above, "TEST 1" doesn't work however it seems to be the most logical format.

"TEST 2" does work, but it always redirects, and I want it to only redirect when the target file is missing (during a 404).

1 Answers
RewriteRule ^images/product_(\d+)_small.jpg     images/missing_(\d+)_small.jpg [L]
RewriteRule ^images/product_(\d+)_large.jpg     images/missing_(\d+)_large.jpg [L]

You need to check that the requested file does not exist before rewriting the request. These two rules can also be combined into one.

The substitution string (2nd argument) is also erroneous. From your example, it should be 0, not (\d+) (which is a regex and has no place here).

For example:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^images/product_\d+_(small|large)\.jpg$ images/missing_0_$1.jpg [L]

The $1 backreference contains either "small" or "large" captured from the RewriteRule pattern against the requested URL-path.

Note that this is an internal rewrite. The "missing" image is displayed with a 200 OK response, not a 404.


#TEST 1:
RedirectMatch 404 ^images/product_(\d+)_small.jpg   images/missing_(\d+)_small.jpg
RedirectMatch 404 ^images/product_(\d+)_large.jpg   images/missing_(\d+)_large.jpg

There are several problems here.

  1. By serving a 404. You are doing just that... serving a 404 only, not "redirecting" to the desired "missing" image. In fact, in this instance, the very presence of the 3rd argument (the target URL) would actually trigger an Internal Server Error (500 response) on Apache. (I assume that's what you mean by "doesn't work"?)

  2. The URL-path matched by the RedirectMatch directive starts with a slash, so these directives will not match the requested images.

  3. Using RedirectMatch it's not possible to determine whether the requested image exists or not (you need to use mod_rewrite, ie. RewriteRule and RewriteCond).

  4. Using RedirectMatch you would need to trigger an external redirect to the required image. (You need to use mod_rewrite to internally rewrite the request.)

Related