Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show no_picture.png by .htaccess if picture is not exist

I want to show no_picture.png if requested picture does not exists. I should do it with .htaccess. Thanks a lot.

like image 574
mTuran Avatar asked Sep 09 '09 05:09

mTuran


3 Answers

RewriteEngine On
RewriteCond %{REQUEST_URI} \.(jpg|jpeg|gif|png|ico)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .*$ /no_picture.png [L]

Let's break it down as to what each line means.

RewriteCond %{REQUEST_URI} \.(jpg|jpeg|gif|png|ico)$ [NC]

Check to see if the requested file is of a file extension in the parentheses (). In this case, we're testing to see if the file name ends in either .jpg, .jpeg, .gif, .png or .ico

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

Check that the file is not there and it's also not a directory.

RewriteRule .*$ /no_picture.png [L]

If a requested resource/file passes all those tests, then it's an image that does not exist. So serve back the image of no_picture.png to the browser. This will keep the filename. If you want to redirect to the no_picture.png filename, change [L] to [R]

like image 104
random Avatar answered Nov 04 '22 09:11

random


this should also work:

<FilesMatch "\.(jpg|jpeg|png|gif)$">
ErrorDocument 404 "/no_picture.png"
</FilesMatch>
like image 4
ownking Avatar answered Nov 04 '22 08:11

ownking


RewriteCond %{REQUEST_URI}  pic/(.*)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule pic/(.*)            pic/no_picture.png [L,E=STATUS:404]
like image 1
martin.malek Avatar answered Nov 04 '22 09:11

martin.malek