A way to change image urls in post to cdn image url?

due to some reason my hosting company isn’t able to fix if i use w3 total cache or w3 super cache for caching my site’s sql usage keeps on peaking…. and on high spikes servers gets laggy and down sometimes.

For that i have to use hyper cache plugin for cache as it works perfect, but this plugin doesn’t support cdn .. i have bought a cdn service from maxcdn but for that i wud have to use w3 total cache or super cache…

Read More

So i am looking for a way to automatically change the post images url from for example : http://www.sitename.com/wp-content/uploads….. to http://cdn.sitename.com/wp-content/uploads

is it possible ? if yes please help.
Thanks in advance

Related posts

Leave a Reply

5 comments

  1. You can parse via regex for images in the_content; but is always load and slowly.
    Maybe you change the url of images, after post_save in database or change the current posts inside the database and create an custom CDN. Its the fast way and all caching plugins has the break, that she must parse the content.
    For background an custom CDN in WP see this answer.

  2. Have you looked at this plugin?
    http://wordpress.org/extend/plugins/cdn-rewrites/

    From the description of the plugin:

    Basically, this plugin allows a WordPress user to specify two important variables: an orgin host (your site) and a “destination host” (cdn host). It will then find all the static contents from that origin host and rewrite them into the destination so that they will be delivered from there.

    This seems to be what you need?

    —————————— EDIT

    If trying to avoid using plugins, here is a great article about it:
    http://www.cyberciti.biz/tips/wordpress-cdn-content-delivery-network-configuration.html

    However, I have never tried this method. But it looks like a clean solution.

  3. if the images remain in the same location, and you’re only wanting to change the subdomain, .htaccess RewriteRule would be the easiest:

    if the rule only applies to images, then:

    RewriteCond   %{HTTP_HOST}   =www.sitename.com
    RewriteRule   ^.+.(jpe?g|png|other|img|ext)$   http://cdn.sitename.com/$0   [nocase,redirect=temp,last]
    

    you could add a RewriteCond test for the images, but the test being done in RewriteCond uses the same resources as doing it once in RewriteRule.

    you could also redirect everything in the uploads directory. if cdn.sitename.com is going through the same .htaccess rule set, I’d include the RewriteCond statement:

    RewriteCond   %{HTTP_HOST}   =www.sitename.com
    RewriteRule    ^wp-content/uploads/.+$   http://cdn.sitename.com/$0   [nocase,redirect=temp,last]
    

    [redirect=temp] or [redirect=permanent]; often written as [R=302] and [R=301].

    apache mod_rewrite documentation

    (the automatic syntax hiliting/formatting was not designed with .htaccess in mind 🙂

    cheers,
    Gregory

  4. if you want to hardcode the img src changes, this would be the function for you; wp_update_post( $post ); coupled with a loop that goes through every post and post_type that you want to affect, and using preg_replace to replace the src url’s.

    assign the template to a page, load the page whenever you want to update the src url’s, delete the page whenever you don’t want to allow others to run the code.

    the grep_replace() search/replace patterns would look something like this:

    search: (<img [^>]*?src=['"]https?://)www.sitename.com
    replace: $1cdn.sitename.com

    or more specifically:

    search: (<img [^>]*?src=['"]https?://)www(.sitename.com/wp-content/uploads/)
    replace: $1cdn$2

    even though wp_update_post() creates a revision automatically if case you need to reverse the change, backup the database before you load the page.

    I’d test the loop, wp_update_post() and preg_replace() combination on a limited found set of posts first, possibly specifying the subject posts’ IDs in an array and stepping through that array.

    an additional option might be to create a new temporary category ‘CDN Updated’ and assigning the new category to each post as you update it. you could then omit any posts with this category term from future update queries. the one small disadvantage of this is that even if you later delete the category, the values would still be saved for each post in the db (correct me if I’m wrong).

    cheers,
    Gregory

  5. you could also hook into the pre_post_update action.

    the benefit of this is that your server just needs to exchange the urls once – so you don’t always use the str_replace script you wrote on delivery, but on the save.

    as i assume you are familiar with the replacing itself, just put your function into this function, and there you go!

    add_action('pre_post_update', 'change_image_urls', 10 );
    
    function change_image_urls( $post_id ) {
    
        // your function here...
    
        // $post = get_post( $post_id );
    
    }