homepage Welcome to WebmasterWorld Guest from 54.227.12.4
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
Load images from other server when they do not exist
tntpower




msg:4082647
 12:21 am on Feb 18, 2010 (gmt 0)

I recently move a site to a new server. Some images are not moved.

For HTML markup like

<img src=/images/hello.gif" />

Suppose this image was not moved to new server. I want new site to load the image from old server automatically.

In other words, all visits (to images) that can trigger a 404 error will automatically call the remote server to get the file.

I want something that

For
<img src=/images/hello.gif" />

If image does not exist

Then load
<img src=http://old_server/images/hello.gif" /> on the fly.

Can I?

Thanks,

 

jdMorgan




msg:4082684
 1:38 am on Feb 18, 2010 (gmt 0)

Yes, you can do this if "the other server" has a unique hostname (domain, subdomain, or public IP address). But you will need to check each and every image request arriving at the new server to see if the file exists, and this will slow down your entire site.

It isn't at all clear why you don't simply move all the images for the site, as that would certainly be the simple and most efficient thing to do...

Jim

bcc1234




msg:4082710
 2:50 am on Feb 18, 2010 (gmt 0)

Look into custom error handling, mime-types, and conditional redirects.

Basically, if a 404 is about to be returned for the image type (or go by location instead of mime type), then instead return a 301 for the old host with the same file name.

Most browsers would follow the redirect for images and load it from the old host.

jdMorgan




msg:4082723
 3:08 am on Feb 18, 2010 (gmt 0)

There's a simple --but very inefficient-- solution: It takes two lines of code in mod_rewrite.

Jim

tntpower




msg:4082753
 4:19 am on Feb 18, 2010 (gmt 0)

Re JdMorgan:

It isn't at all clear why you don't simply move all the images for the site, as that would certainly be the simple and most efficient thing to do...


Hi Jim,

It is because all files on the old site are static HTML files. I moved the site to Drupal. Have not got a time to update image src in Drupal yet.

Next time before moving to Drupal, I think I need to use regular expression to do a site wide search and replace on the old site to update all image src.

Thanks,

jdMorgan




msg:4082907
 2:06 pm on Feb 18, 2010 (gmt 0)

A multi-file search-and-replace would be a very good idea.

The following code snippet will work as a temporary work-around, assuming that you already have other working mod_rewrite rules in place, and that you place it before all of your existing redirects (because the target domain is different), and that all of your redirects are coded using mod_rewrite and have not mixed in mod_alias "Redirect" and/or "RedirectMatch" directives. In this latter case, there is no way to predict whether the mod_rewrite or mod_alias directives will be processed first, since directive invocation is per-module, and not in strict line-by-line order.

As previously stated, the old server must be publicly- and independently-accessible either via domain name, subdomain, or IP address.

# Redirect requests for missing images to old server
RewriteCond $1 !^excluded-local-image-directory-path-1/
RewriteCond $1 !^(excluded-local-image-directory-path-1|excluded-image-directory-path-2)/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+\.gif)$ http://www.old-example.com/$1 [R=302,L]

I included two 'token' RewriteConds at the top. If possible, put the name(s) of the new sites image directory-paths into these RewriteConds, so that the file-exists checking can be skipped if the image can be safely presumed to exist on the new site based on its URL-path.

I show two forms, the first excluding one directory-path per RewriteCond, and the second using a "local OR" to exclude more than one local image-directory-path. You may use either or both forms, adding more RewriteConds in these formats if needed. Do not end-anchor these patterns or they won't work.

Alternately, if the directory paths for missing images are all the same or if there are only a few, you may wish to use a positive-match pattern either in RewriteConds or in the RewriteRule pattern itself, so that only images in directory-paths which are likely to be missing will be checked. The decision should be based upon which approach results in the fewest directory-path inclusion/exclusion directives and/or the simplest and smallest code. However, even excluding or including several dozen directory-paths will still likely be faster than one file-exists check, so use as many as you need to avoid unnecessary disk checking.

The rule above only redirects requests for missing .gif files. If you wish to include other types, its a simple matter of including them in the RewriteRule's pattern, e.g. "^(.+\.(gif|jpe?g|png|ico))$". However, be aware that the more image-types you have to check, the more disk checks you will invoke, and the slower this code will get; Include only the filetypes that are necessary, and put them in order, most- to least-used (check your "stats").

Using the local image directory exclusions/inclusions may make the site's loading speed tolerable, despite all the file-exists checking and client redirects for 'missing' images, but I doubt that you or your visitors will be very happy with the performance of this solution if there are lots of missing images.

Jim

tntpower




msg:4083137
 9:34 pm on Feb 18, 2010 (gmt 0)

Jim,

Thanks a lot for your help. Your code above does work like a charm.

I will do a database update to search and replace all image SRCs.

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.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved