Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I have written a on-the-fly thumbnail creator by an htaccess file looking to see if the requested file exists and if not sending the user to a php page to create it. My htaccess file looks like this:

# Check the formatting (this works)
RewriteCond %{SCRIPT_FILENAME}%{QUERY_STRING} /([a-zA-Z0-9-]+).(jpg|gif|png)w=([0-9]+)&h=([0-9]+)(&c=(true|false))

# Only forward to thumbnail.php if file doesn't exist
#RewriteCond %{REQUEST_FILENAME} !-f   #this works but not for the correct filename!
RewriteCond %1-%2-%3-%4-%6.jpg !-f     # Doesn't work!!!

# If file doesn't exist, send here: (this works)
RewriteRule ^.*$ thumbnail.php?file_name=%1&type=%2&w=%3&h=%4&c=%6

My check of the file's existence does not seem to work however... the old version that uses %{REQUEST_FILENAME} works, but that is not the correct filename I need to check. I have verified that the new file %1-%2-%3-%4-%6.jpg has the correct output, but does not trigger the condition!

Based on this URL:


The htaccess file should check if this file exists:


The thumbnails and htaccess file are both in the same folder, but this folder is unknown and can't be hardcoded. Does it need the full path to check? If so, how can I get the full path but with my custom filename?

Pulling my hair out with this one... PLEASE help! Thank you.

share|improve this question

1 Answer 1

up vote 1 down vote accepted

You are doing it completely wrong (IMHO). This should do the job for you.

RewriteCond %{QUERY_STRING} ^w=([0-9]+)&h=([0-9]+)(&c=(true|false))
RewriteCond %{DOCUMENT_ROOT}/$1-$2-%1-%2-%4.jpg !-f
RewriteRule ^([a-z0-9\-]+)\.(jpg|jpeg|gif|png)$ thumbnail.php?file_name=$1&type=$2&w=%1&h=%2&c=%4 [NC]

1) I have tested it and it works but I may be missing something that is only applicable to your setup/system.

2) This assumes that thumbnail images (e.g. this-is-the-image-name-gif-200-100-true.jpg) are located in the root folder of the website (e.g. if site is located /var/www/http then it check for the following file: /var/www/http/this-is-the-image-name-gif-200-100-true.jpg)

3) If your thumbnail images are located in /thumbnails subfolder then you will need to make some small changes to the lines #2 and #3.


I saw your original question: http://stackoverflow.com/questions/6302529/htaccess-cant-check-dynamic-file-existence

Accordingly to your notes there the generated thumbnail images will be stored together with the thumbnail.php in /media/thumbnails/ folder. Based on this info the rules can be rewritten in the following way:

RewriteCond %{QUERY_STRING} ^w=([0-9]+)&h=([0-9]+)&c=(true|false)
RewriteCond %{DOCUMENT_ROOT}/$1/$2-$3-%1-%2-%3.jpg !-f
RewriteRule ^(media/thumbnails)/([a-z0-9\-]+)\.(jpg|jpeg|gif|png)$ /$1/thumbnail.php?file_name=$2&type=$3&w=%1&h=%2&c=%3 [NC]

These rules need to be placed in the website's root .htaccess file.

You still may play around to make it independent from the folder name (I think you may need to insert another RewriteCondition after first one to check the relative path against %{REQUEST_URI}) but it is very difficult for me to play myself with imaginary files without having already made infrastructure ready.

share|improve this answer
Thanks, the issue is that this is for an installable system and the file path is unknown. All that is known is that the thumbnail.php, images and htaccess file will all be in the same directory (thumbnails). Your method will work, but not given that aspect of the requirements. –  RANGER Jun 17 '11 at 16:57
Well ... you have to have FULL file name in order to check if that file does exist. Even this rule RewriteCond %{REQUEST_FILENAME} !-f does work the same -- %{REQUEST_FILENAME} is resolved to FULL file name automatically. –  LazyOne Jun 17 '11 at 17:14
I see 2 choices here: 1) Your code to have have some sort of install.php and it should generate appropriate .htaccess file with correct paths during setup step; 2) Improve these rules a bit more -- but for this I need to know where thumbnails fill be stored (what sub-folder) + what will be the web path when accessing them + the path to thubmnail.php file. There cannot be universal solution (one-solution-that fits-everyone) under such circumstances with not enough info provided. –  LazyOne Jun 17 '11 at 17:16
I have updated my post based on the folder structure you mentioned in another question: stackoverflow.com/questions/6302529/… –  LazyOne Jun 17 '11 at 20:18

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.