Hi, I want to show no_picture.png if requested picture does not exists. I should do it with .htaccess. Thanks a lot.
A:
RewriteCond %{REQUEST_URI} pic/(.*)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule pic/(.*) pic/no_picture.png [L,E=STATUS:404]
martin.malek
2009-09-09 06:12:51
A:
In your /images/ directory, add this to your .htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule no_picture.png [L]
That says, if the requested file doesn't exist, rewrite it to no_picture.png
Brenton Alker
2009-09-09 06:14:04
Won't that include non-existent HTML or other file types?
random
2009-09-09 06:14:55
yes, it would for anything. Which is why I mentioned it was aimed at a specific images directory. It could be changed to match only certain file extensions (as other answers have done).
Brenton Alker
2009-09-09 07:03:07
+3
A:
RewriteEngine On
RewriteCond %{REQUEST_URI} \.(jpg|gif|png)$ [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|gif|png)$ [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
, .gif
or .png
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]
random
2009-09-09 06:26:01