Caching remote images locally with mod rewrite and PHP
Sometimes you need to load images from a remote server. In my case it was a customer site that used an external image service. The only problem was that the image service was a bit unreliable and slow - which made the customer’s site look slow.
We decided to make a local cached copy of the images, and the solution was simple enough to share here. The website was running on Apache and we took advantage of the mod_rewrite module by placing the following in our .htaccess file.
This means that all requests to /images/ that does not match a real file hits our images/cache.php with the image request path as the querystring.
In our production code we have some more security measures to see that the filename is a valid path, matches some given filenames and does not contain strange characters. But this will give you a nod in the right direction if you need to build something similar.