Support » Plugins » 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”, “” or “”) and rewrite them to a different domain (i.e. “”)

    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', '$$2', $content);
    $content = preg_replace('/(\<img\s+.*?src\=["\'])(\/.*?["\'].*?\>)/i', '$1http://'.$_SERVER['HTTP_HOST'].'$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.

Viewing 2 replies - 1 through 2 (of 2 total)
  • technabob, did you have any luck with this? I’m trying to do something very similar.

    Zack Katz


    In your .htaccess file, you should do something like this:

    Options +FollowSymlinks
    RewriteEngine On
    RewriteRule ^(.+)$$1 [NC]

    Go here to find more comprehensive information about using .htaccess.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘need help rewriting URLs for CDN’ is closed to new replies.