need help rewriting URLs for CDN
-
I need some assistance. I’m trying to create a plug-in for my wordpress blog which would let me do the following…
Look for all <img src=””> tags that are either internal or specifically reference my domain (i.e. “/images/blah.gif”, “https://technabob.com/images/blah.gif” or “https://www.technabob.com/images/blah.gif”) and rewrite them to a different domain (i.e. “https://static.technabob.com/images/blah.gif”)
This is so my images can be served by an external CDN.
So what I really want to do is just rewrite the actual URLs that come out in the HTML rendered by WordPress, and let the end client do all the work of fetching the images from the CDN.
I’ve tried doing something with .htaccess and mod_rewrite, but the problem is that the CDN actually needs to grab the initial image on the first request, so if I put the rule in mod_rewrite, it never gets the image.
The Coralize plugin does something similar, and I’ve been trying to hack that for my purposes, but my RegEx skills aren’t strong enough to properly rewrite their markup.
Here’s the example code from Coralize…
$preg_host = preg_quote($_SERVER["HTTP_HOST"], '/'); $content = preg_replace('/(\<img\s+.*?src\=["\']http\:\/\/'.$preg_host.')(\/.*?["\'].*?\>)/i', '$1.nyud.net:8080$2', $content); $content = preg_replace('/(\<img\s+.*?src\=["\'])(\/.*?["\'].*?\>)/i', '$1https://'.$_SERVER['HTTP_HOST'].'.nyud.net:8080$2', $content);
I just need some help to rewrite those preg_replace statements to do what I mentioned in the first paragraph.
Any help would be greatly appreciated.
- The topic ‘need help rewriting URLs for CDN’ is closed to new replies.