I must set all image references (really just references to some specific folder),

e.g. mywedomain.com/images/myimage.jpg, where mywebdomain.com/images/myimage.jpg would really indicate mycdn.com/images/myimage.jpg.

But keep your internal reference within the code/markup itself. I'm wondering how you might handle this?


It is dependent if it's on a single server or otherwise regarding exactly how you need to handle that.

If it's on a single server, and inside the document root, it might be simplest having a rewrite, using mod_rewrite.

Within your primary directory .htaccess file:

RewriteEngine on
RewriteBase /

RewriteRule ^/images/(.+)$ /mycdn.com/images/$1 [L]

If it's not within the document root, or on another server, a redirect may be simpler.

RedirectMatch ^/images/(.+)$ http://mycdn.com/images/$1 

Though this is noticeable around the client, when they were searching in the request log. If you would like so that it is transparent, then you may proxy the request in your server.

Produce a PHP file (or any other language you utilize, you mention Light, hence PHP).


$reqImage = $_GET['image'];
$mimeTypes = array('gif' => 'gif', 'jpg' => 'jpeg', 'jpeg' => 'jpeg',
                   'png' => 'png');

$matches = array();
if (preg_match("/^[-a-zA-Z0-9_]+\.(gif|jpg|png)$/", $reqImage, $matches)) {
   header('Content-Type: image/' . $mimeTypes[$matches[1]]);
   @readfile("http://mycdn.com/images/" . $reqImage);
} else {
   header('HTTP/1.0 404 Not Found');


Note the necessity to distribute the Internet media type when supplying the look, as Apache is going to be expecting different things from the PHP file, and normally handles that for you personally.