homepage Welcome to WebmasterWorld Guest from 54.211.190.232
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Accredited PayPal World Seller

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
Mod Rewrite & Missing Images
emma matthews




msg:1523598
 10:28 am on Apr 25, 2003 (gmt 0)

Regarding this post:
[webmasterworld.com...]

which is now closed, so I cannot post a reply there, can anyone help, please?

The following code was posted to replace a missing image with a default one:
Options +FollowSymLinks
RewriteEngine on
# Redirect missing catalog images to a default image. Let other missing resource requests 404 normally.
RewriteCond %{REQUEST_URI}!-U # if requested resource doesn't exist
RewriteRule ^catalog_image_path/.*\.gif$ /default_image_path/default.gif [L] # redirect to default image

This, in my understanding, replacing missing images only in the specified directory. However, what I need to do is replace all missing images be they .gif, .jpeg whichever directory they may be in or even if they are on a different server(or website). Is there any way I can achieve this?

Thanks In Advance.

 

john316




msg:1523599
 12:44 pm on Apr 25, 2003 (gmt 0)

>even if they are on a different server(or website)

Mod rewrite will only effect your server.

grahamstewart




msg:1523600
 12:51 pm on Apr 25, 2003 (gmt 0)

To make the rule work for all image type in all directories I think you just need to just change this line..

RewriteRule ^catalog_image_path/.*\.gif$ /default_image_path/default.gif [L] # redirect to default image

to

RewriteRule \.(gif¦jpg¦jpeg)$ /default_image_path/default.gif [L] # redirect to default image

That should work (I don't know mod_rewrite too well - but I do understand regular expressions :))

jdMorgan




msg:1523601
 4:25 pm on Apr 25, 2003 (gmt 0)

Emma,

Yes, grahamstewart's got it.

Some more info, though: In the original, I added comments on the code lines to illustrate the flow, but this will cause warnings in most Apache installs. Move the comments to their own lines as shown below or just remove them entirely.

Options +FollowSymLinks
RewriteEngine on
# Redirect missing catalog images to a default image. Let other missing resource requests 404 normally.
#
# if requested resource doesn't exist
RewriteCond %{REQUEST_URI} !-U
# redirect to default image
RewriteRule \.(gif¦jpg¦jpeg)$ /default_image_path/default.gif [L]

This code uses the "-U" hook to determine if a URL exists on the site. As such, it is fairly inefficient; see the note in the original thread. There's no way around the inefficiency, so make sure the RewriteRule is as exclusive as it can be. If the RewriteRule does not match, then the RewriteCond will not be evaluated, saving a wasted URL-exists lookup. Also, place this code as close to the top of your .htaccess as possible - It will be processed for every image request!

If you have a large, busy, image-intensive site, monitor it closely for perfomance issues after installing this code. Also, make sure you have made your images cacheable by including correct cache-control headers. This will reduce demand on the image-exists lookups for any images which are used repeatedly (spacer gifs, logos. etc.).

As always, replace the broken vertical pipe "¦" characters above with the one from your keyboard.

HTH,
Jim

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About
© Webmaster World 1996-2014 all rights reserved