.htaccess, default to full size image if thumbnail does not exist
New here? Learn about Bountify and follow @bountify to get notified of new bounties! x

I have a process that uploads full-size images into a folder via FTP.
A scheduled task monitors this folder and creates thumbnail versions of these images.
This creates a period of time where the full size image exists, but the thumbnail does not.

I would like to create a rule in my .htaccess file that does the following...
If a thumbnail image is requested, but that file doesn't exist, serve the full-size version instead.

An example of this is https://www.rundeautogroup.com/wp-content/uploads/vehicles/thumbs/IMG_8517.JPG... This file does not exist, so I'd like anyone who requests it to get https://www.rundeautogroup.com/wp-content/uploads/vehicles/IMG_8517.JPG instead

Here's the .htaccess file I placed into the "thumbs" folder, but it doesn't seem to be doing anything...

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^wp-content/uploads/vehicles/thumbs/(.*)$ https://www.rundeautogroup.com/wp-content/uploads/vehicles/$1 [L]


Crowdsource coding tasks.

3 Solutions


Depending on your apache version (>2.2 ?), you should add %{DOCUMENT_ROOT} to your condition. Please check https://www.harecoded.com/apache-rewritecond-f-check-file-exists-solution-2246468/ for a demo ans have a try.


Sorry I tried my solution too quickly, "DOCUMENT_ROOT" part is not necessary. I read again your problem, and the issues seems to be a path issue.

As you are in the "thumbs" directory your rewrite rule origin must be an absolute path beginning by "/"

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/wp-content/uploads/vehicles/thumbs/(.*)$ /wp-content/uploads/vehicles/$1 [L]

If this doesn't work, you can try to set this .htaccess on the root folder of the server.

For information, I removed the domain in the destination path as you are on the same server, it is not necessary to specify it.

I tried that, but still not getting what I want.. Here's what I tested... `# BEGIN image checker RewriteEngine on RewriteCond %{DOCUMENTROOT}%{REQUESTFILENAME} !-f RewriteRule ^wp-content/uploads/vehicles/thumbs/(.*)$ https://www.rundeautogroup.com/wp-content/uploads/vehicles/$1 [L]

END image checker`

Also, this syntax throws an error on the htaccess tester at https://htaccess.madewithlove.be/. Maybe I have something wrong there?
Matt Connolly 9 months ago

Since you have placed the .htaccess file inside thumbs folder, so you need to alter the path as I have mentioned below:-

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ https://www.rundeautogroup.com/wp-content/uploads/vehicles/$1 [L]

I have already tested the same on my local machine and keeping it in the thumbs folder and its working absolutely fine now. Sometimes, the system gets stuck, so please don't forget to restart apache in your server in that case.


Hey, thank you for this... The .htaccess is at least doing something now, but not quite what I want. When I try to view an image that doesn't exist (i.e. https://www.rundeautogroup.com/wp-content/uploads/vehicles/thumbs/IMG_8517.JPG) it redirects me to a blog post on my site rather than the full size image. On the plus side, when I view an image that does exist in that folder (https://www.rundeautogroup.com/wp-content/uploads/vehicles/thumbs/01_KFA35913.JPG) it does not redirect which is what I expected Based on the test I ran at https://htaccess.madewithlove.be/, it seems like I'm being redirected to "https://www.rundeautogroup.com/wp-content/uploads/vehicles/https://www.rundeautogroup.com/wp-content/uploads/vehicles/" (i.e. the URL twice)
Matt Connolly 9 months ago
Hello, Thanks for checking. I have gone through your message. To check the way you did, I visited the site https://htaccess.madewithlove.be/ and did the test the way you mentioned and although initially, I was getting the URL twice as you mentioned later I started getting the same URL back while I was checking, I guess your auto system generated the thumbnail of the image while I was checking. Another major hurdle is that I can't access the image link or the site URL as it's restricted from India. If you can open the access to the site while we are debugging then it would make it easier for us to fix it. Please also note that there could be other .htaccess files configured on the server that might be conflicting with the desired output. Thanks
topgeek 9 months ago


probably too late, but try this (note there are no leading slashes):

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^wp-content/uploads/vehicles/thumbs/(.*)$ wp-content/uploads/vehicles/$1 [L]

Your testing service seems to be ok with this.

View Timeline