WordPress.org

Ready to get started?Download WordPress

Forums

need help rewriting URLs for CDN (3 posts)

  1. technabob
    Member
    Posted 5 years ago #

    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", "http://technabob.com/images/blah.gif" or "http://www.technabob.com/images/blah.gif") and rewrite them to a different domain (i.e. "http://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', '$1http://'.$_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.

  2. dmisener
    Member
    Posted 5 years ago #

    technabob, did you have any luck with this? I'm trying to do something very similar.

  3. Zack Katz
    Member
    Posted 5 years ago #

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

    Options +FollowSymlinks
    RewriteEngine On
    RewriteRule ^(.+)$ http://static.technabob.com/$1 [NC]

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

Topic Closed

This topic has been closed to new replies.

About this Topic