Forum Moderators: phranque

Message Too Old, No Replies

show alternate image if image-src is broken

         

quickstudy

3:23 pm on Aug 17, 2005 (gmt 0)

10+ Year Member



Hi,

if I, on my webpage, has an image-tag with no image to show, I would like to show an alternate image.

at the moment I have this:

RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}!-f
RewriteRule .*\.(gif¦jpg¦jpeg¦bmp)$ img/notfound.jpg

but it doesn't seem to work.

I am quessing it's the %{request_uri} that should be replaced, but with what?

jdMorgan

5:09 pm on Aug 17, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



quickstudy,

Welcome to WebmasterWorld!

REQUEST_FILENAME. If you change it, you may or may not need the DOCUMENT_ROOT path in front of it.


RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-f
RewriteRule \.(gif¦jpe?g¦bmp)$ /img/notfound.jpg [L]

Also, add an [L] flag as shown, and I've removed unnecssary tokens from your RewriteRule pattern. Change the broken pipe "¦" characters to solid pipe characters before use; Posting on this board modifies them, and non-ISO-8859 character-set browser settings may further modify these characters (and others).

Use of the "file exists" check is inefficient. I'd suggest trying to fix as many broken images as possible before choosing to deploy this code. If there is any way to make the RewriteRule pattern more specific or to restrict the scope of this rule in other ways --for example if your images are all located in a subdirectory-- then do it.

Jim

quickstudy

1:33 pm on Aug 18, 2005 (gmt 0)

10+ Year Member



Thanks for the welcome :)

I now changed my scritp to

RewriteCond %{REQUEST_FILENAME}!-f
RewriteRule ^.*\.(jpe?g¦bmp¦gif)$ /img/notfound.jpg [L,NC]

and it seems to be working

but...

what do you mean by

'Use of the "file exists" check is inefficient'?

I don't know of any other way to get the desired effect.!

regarding restricting the code.

I would like the code only to affect images that are placed on my pages.
meaning:
if you click an "image-link" on a page, it links to the image itself.
if the image doesn't exist, then with this code, the "notfound.jpg" image is displayed (alone).

what I would like is:

if the image doesn't exist, a 404 error occurs, and an E-mail is sent to me, informing me of the broken link/img-src

(Email is sent from my custom 404-errorpage)

I know that I should place another rewriteCond in the code, but I dont know what to test for, since I don't know exactly what the different %{...}-variables return

/Kasper

jdMorgan

2:50 pm on Aug 18, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



what do you mean by
'Use of the "file exists" check is inefficient'?
I don't know of any other way to get the desired effect.!

I mean that it invokes an additional call to your file manager (in the operating system) for each image requested from your server. So it is good to limit the "scope" of the rule --limit the circumstances where it is applied-- to only those requests where it is absolutely necessary. This can be done, for example, if only certain image types need to be checked, or if only images in a specific URL-path need to be checked. Otherwise, you run several hundred lines of code, and possibly have to access the disk, for every image requested from your site.

The trade-off is between usability and server performance, and only you can decide what a reasonable balance is. Consider *why* an image might be missing, and that may provide a way to limit the scope.

what I would like is:

if the image doesn't exist, a 404 error occurs, and an E-mail is sent to me, informing me of the broken link/img-src

(Email is sent from my custom 404-errorpage)


This requires that you rewrite missing-image requests to a script that will send the e-mail, and then read in the replacement image file, add the proper HTTP response headers (including MIME-type), and send it to the client. That's a bit beyond the scope of the Apache forum, though.

Jim

quickstudy

3:43 pm on Aug 18, 2005 (gmt 0)

10+ Year Member



I have removed the working code from my server and I'll try to limit the scope before I upload my code agian.

thanks for your replies

/Kasper