htaccess URL rewrites with multiple Regular Expressions

Viewed 27

thanks for your time. I thought I knew how to do this but I've tried dozens of permutations and I'm failing.

I want to replace missing images with a placeholder image of the appropriate size. The real images are organized into directories by size. Directory names are just numbers such as: 320, 640, 960, 1080.... Each size directory contains an appropriately sized image, all called "toc-placeholder.jpg"

Sample of missing image URL

/img/toc/640/toc-074.jpg 

Path to placeholder image

/img/toc/640/toc-placeholder.jpg 

## PLACEHOLDER FOR MISSING TOC IMAGES ##
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_URI} \.(gif|jpg|jpeg|png)$ 
## REPLACE ANY MISSING UNDER THE "TOC" DIRECTORY WITH ONE SPECIFIC IMAGE##
RewriteRule ^img/toc/* img/toc/960/toc-placeholder.jpg [L]
## REPLACE MISSING IMG WITH IMAGE FROM THE SAME SIZE DIRECTORY##
##RewriteRule ^img/toc/([0-9]+)/([a-zA-Z0-9_-]+)$ img/toc/$1/toc-placeholder.jpg [L]

The first rule (the active one) works fine. Id like to replace it with the second rule (the one that is commented out) so I can redirect to a placeholder of the appropriate size in each case.

Any help would be greatly appreciated.

1 Answers

I've got it working.

It occurred to me that the expression: ([a-zA-Z0-9_-]+) alone might not account for file extensions. I tried to get a . into the that expression but had trouble so I modified the line to add file extensions thusly:

## PLACEHOLDER FOR MISSING TOC IMAGES ##
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_URI} \.(gif|jpg|jpeg|png)$ 
## RewriteRule ^img/toc/* img/toc/960/toc-placeholder.jpg [L]
## PLACEHOLDER FROM APPROPRIATE SIZE DIRECTORY (NOT WORKING) ##
RewriteRule ^img/toc/([0-9]+)/([a-zA-Z0-9_-]+)\.(gif|jpg|jpeg|png)$ img/toc/$1/toc-placeholder.jpg [L]

Seems like there's an easier way to do this if anyone wants to share. Thank you for your help.

Related