The 302 doesn't happen for local urls e.g. friendly urls in Forums, Blogs etc. I use url rewriting on all of my sites, it doesn't do a 302 when the target is Local. But Rewriting to remote urls is treated differently... which I've just learnt.
Correct. When you are rewriting to local, the caller (browser) has no idea - you're simply modifying how the call is passed through the Apache chain. But when you add http:// to the front, you are sending back a message (a 302) to tell the browser that the resource it needs is located elsewhere. You can make it a 301 as well with parameters on the end of the rule.
Yea, they are all my sites, including the target site. But I want to completely "separate" the sites without having to upload the same image folder for every site. In fact, image urls are read from a db and display from a central location for all sites. My intention was to have the image locations "appear distinct" without duplicating the image folder.
I think you may have misunderstood me.
I do this all the time and it rocks. I use it both for mini-mashups and to reduce load on the servers that create my HTML. Assuming you have a PHP script that dumps out all the html in one place, you can easily rewrite each call for an image to look to the correct location (your central repository) and then the server doesn't need to tell the browser where to get the resource. This will reduce your server overhead considerably and make pages pop much more quickly, because the browser will open multiple sockets to multiple locations.
Let's assume your HTML looks like this:
blah blah blah <img src="/images/test.jpg">
Again, at the very last moment before the HTML is shipped out to the caller, we do a quick replace:
// assume $content contains the entire HTML page set to go out:
$content = preg_replace('~<img src="/~', '<img src="http://www.myrepository.com/images/', $content);
Now the browser knows where to get each image without having to request it from you. WAY better on your bandwidth and perceived page speed.
I was thinking you might simply add something like:
if (! $buff = file_get_contents("/www/cache/$img"))
file_put_contents($buff = file_get_contents("http://remotedomain.com/img/$img"), "/www/cache/$img");
header('Content-type: image/' . $imgtype);
echo file_get_contents('http://remotedomain.com/img/' . $img);
... then you still get the benefit of the remote repository and caching the images locally. But I think the above method is better.