So I am writing an on-the-fly thumbnail generator for a CMS and I am trying to find the best way to handle requests. Specifically, I need to come up with the apache code for an htaccess file that will take all requests to a thumbnail folder and send them to a thumbnail.php file instead.
Ideally it would only forward you to thumbnail.php if the thumbnail doesn’t exist, although checking for file existence within apache is beyond me.
The request URL would look like this:
http://unknown_domain.com/unknown_folder/media/thumbnails/image-name.jpg?w=200&h=100&c=true
And images would be stored in the thumbnails something like this (or could be the same URL as below if that makes more sense):
http://unknown_domain.com/unknown_folder/media/thumbnails/image-name-200-100-true.jpg
Here is the closest I’ve come so far, I assume I put this htaccess in the thumbnails folder:
RewriteEngine on
#RewriteBase /unknown_folder/media/thumbnails/ # optional, depends on your setup
#Check for file existence here and forward if exists
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z-])-([0-9]+)-([0-9]+)-true.jpg$ thumbnail.php?w=$2&h=$2&c=true [LQSE]
Thank you for any help provided in advance!
I guess you’re looking for a regex:
I have not tested it, but ideally a request to
should call the script
thumbnail.phpwith it’s parameters.Edited: Fixed some typos and errors.