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...

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

5 Answers 5


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.

  • Thanks for pointing in the right direction i created my own function by just using the str_replace.. in the content. and it worked i wil post it here in a few mins
    – Ayaz Malik
    Apr 29, 2012 at 23:22

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.

  • 1
    An answer should be more than a link to an external site. Please add some information to your answer: What the plugin does and how it works. Thanks.
    – fuxia
    Apr 19, 2012 at 17:16
  • Thanks i will try that but im trying to find a way without using any plugin
    – Ayaz Malik
    Apr 19, 2012 at 21:47
  • maybe look at the code in the plugin and see if you can digest it into your theme. it is pretty straightforward plugin
    – Starfs
    Apr 20, 2012 at 2:06
  • i added the plugin but it broke my admin panel lol.. not able to add tags anymore
    – Ayaz Malik
    Apr 20, 2012 at 10:12
  • 2
    that is an lol. the plugin adds jquery again, which is unnecessary and jqueryui, which you may be already running. i had to remove the enqueuing on it because it was causing issues. you can comment out those lines in cdnr.class.php - not sure if that is what is causing your issue though..
    – Starfs
    Apr 20, 2012 at 15:33

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 :-)



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).


  • this is a great solution, gregory! although i really recommend doing the replace directly before the save-action, as you do not have to worry about new posts afterwards. if your site has a lot of posts, a function that loops through all posts again and again takes a lot of time to execute.
    – fischi
    May 2, 2012 at 7:12
  • :-) fischi, that's why I recommend placing the code into a specialised template, assigning the template to a Page, and only loading the Page when you want to run the update function. the programmer could even include a role check in the template to only allow Admins to load the Page.
    – Gregory
    May 2, 2012 at 12:04
  • oh, i see. i thought you wanted to run the update on the frontend template of the page, my mistake ;) you are also right with the category, including the minor drawback - but combining our methods you would not need to run the script again afterwards :)
    – fischi
    May 2, 2012 at 12:46

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 );

  • yes makes sense but considering 990 posts already published i am not sure how this will help. so im just using a str replace in a function in functions.php which replaces ..
    – Ayaz Malik
    May 1, 2012 at 22:08
  • okay, i get your point here - maybe you could write a small function to be called from the backend, where you search through all your posts, updating them once? i know it takes a while with the 990 published, but it would be an elegant solution. you could also use the search and replace plugin to perform this action directly in your database.
    – fischi
    May 2, 2012 at 7:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.