Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Default (fallback) Image with .htaccess?

I'm curious if it's possible to use .htaccess to serve up a default image when a particular image is requested that doesn't exist (note: only image-requests should raise this behavior). I know I could do this with PHP by serving the images through a script, but I'm more curious if this can be done with .htaccess instead.

Suppose I request /thumbnails/010.gif, which doesn't exist. How could I get .htaccess to serve up /thumbnails/default.gif in its place?

like image 961
Sampson Avatar asked Dec 07 '09 01:12

Sampson


2 Answers

I believe this will work for you. Place this .htaccess in your thumbnails directory and any URIs below /thumbnails that do not exist will direct to /thumbnails/default.gif

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ default.gif [NC,L]
like image 194
Travis Avatar answered Oct 13 '22 21:10

Travis


Since you just want to have it applied to /thumbnails/:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^thumbnails/ thumbnails/default.gif
like image 27
Gumbo Avatar answered Oct 13 '22 20:10

Gumbo