How to rewrite image url in nodes? I need to rewrite all image urls inside certain node types into custom-defined urls. For example, this image:

&lt;img src="/sites/default/files/u22/pretty.jpg" width="678" height="1024" alt="pretty.jpg" /&gt;


need to be rewritten to:

&lt;img src="http://files.mysite.com/sites/default/files/u22/pretty.jpg" width="678" height="1024" alt="pretty.jpg" /&gt;


And I need to do that for all of the user-generated images. Obviously, the goal is to be able to off-load all images to a secondary file server.
How can I do that? 

Thanks
ok if you're looking at drupal 6 look into custom_url_rewrite_outbound(). For D7, you can use hook_url_outbound_alter().

Some example code I have sitting around in a module that takes media resources of a Drupal site and points the generated urls to another server to serve media content, we used Nginx. Keep in mind that if you're just sending back to same server on another port .... you're just feeding yourself more traffic -- which isn't as helpful as a true CDN.

 /*
 *
 * A mechanism in Drupal to rewrite outbound urls to point to another domain.
 * This can be used to serve static content from a dedicated server, aka a CDN.
 *
 * Taken 100% from the following url. I am not smart enough to pull off this
 * magic :).
 *
 * http://drupal4hu.com/node/260
 *
 * In the below code $conf['dew_cdn_domain'] is a custom value in
 * settings.php that points to like "http://yourotherserver.com" to serve
 * media files.
 */


/**
 * Implementation of hook_file_url_alter()
 *
 * This inspects and transforms file urls to our remote CDN host.
 */
function dew_cdn_file_url_alter(&amp;$uri) {
  global $conf;
  static $drupal_static_fast;
  // A drupal_static_reset() would clear this and then we need to rerun variable_get.
  if (!isset($drupal_static_fast['dew_cdn_domain'])) {
    $drupal_static_fast['dew_cdn_domain'] = &amp;drupal_static(__FUNCTION__);
    $drupal_static_fast['dew_cdn_domain'] = $conf['dew_cdn_domain'];
  }
  $cdn_domain = &amp;$drupal_static_fast['dew_cdn_domain'];
  if ($cdn_domain &amp;&amp; strpos($uri, ':') === FALSE) {
    $uri = $cdn_domain . $uri;
  }
}


/**
 * Implementation of hook_stream_wrappers_alter()
 */
function dew_cdn_stream_wrappers_alter(&amp;$wrappers) {
  $wrappers['public']['class'] = 'DrupalCDNPublicStreamWrapper';
}


/**
 * Any files that would go the public stream public:// get rewritten to another
 * base domain.
 */
class DrupalCDNPublicStreamWrapper extends DrupalPublicStreamWrapper {
  function getExternalUrl() {
    global $conf;
    $path = str_replace('\\', '/', $this-&gt;getTarget());
    return $conf['dew_cdn_domain'] . self::getDirectoryPath() . '/' . drupal_encode_path($path);
  }
}