Mod rewrite help, please

I have over 1 million downloadable products in my online store but I don’t want to have individual images because there would be over 3 million images in this directory.

The product image NAMES would normally formatted like this (this is how they are generated in my store software):

Where XXXXX = the productID and YYYYY = the id of the image record in the image table.

Since I want them to use the exact same image(s) because they are downloadable products, how can I create a mod-rewrite entry where any product image name generated by the store would actually return these?

I’ve tested this, so I know it works, but the RewriteBase value seems clumsy. It feels like I ought to be able to match the path as a pattern but I didn’t have much success. I’ll be interested if someone can shed further light on it.

Options +FollowSymlinks
RewriteEngine on
RewriteBase /demo/sitepoint/htaccessTest/images/
RewriteRule ^[0-9]+-[0-9]+-(.*).jpg$ 1-1-$1.jpg [NC]

You can test the rule by requesting these images (which don’t exist):

David, if I try your version I get a 500 Internal Server Error. Could that be down to server config?

Thanks for the tips.


Thanks. That was a good start. Comments, however:

Options +FollowSymlinks
# that should already be in httpd.conf
RewriteEngine on
# good - not in comment mode
# RewriteBase /demo/sitepoint/htaccessTest/images/
# no, RewriteBase is designed to UNDO a mod_alias redirection
# because the request is to the images directory, this can be in
# the .htaccess there as follows
RewriteRule ^[0-9]+-[0-9]+-(.*).jpg$ 1-1-$1.jpg [[SIZE=+2]L[/SIZE]]

NEVER forget to terminate your mod_rewrite statements!

AND, you don’t need a No Case flag here - Apache path/file IS case sensitive so the NC flag is designed for the {HTTP_HOST} which is NOT case sensitive.



No, it could be that I was interrupted when I answered and removed the brackets around the Last flag (replaced above) and that WILL generate a 500 error. Sorry about that!