Shiba

Adventures in WordPress

  • Home
  • Dog
  • Art
  • Contact
  • WordPress Articles
    • WP Plugins
    • WP Programming
    • WP Admin Panels
    • WP Theme Design
    • WP How-To
    • WP Theme Images
You are here: Home / WordPress General / Setting Up MT ProCDN, W3TC, and WordPress Multisite

Setting Up MT ProCDN, W3TC, and WordPress Multisite

by ShibaShake 8 Comments

Recently, I decided to give Media Temple’s ProCDN system a try. The awesome W3tc caching plugin already supports MT’s ProCDN so it was actually a pretty painless process.

The only real issue that I faced was getting the image links for my directory based multisite setup to translate properly on my CDN. Here is how I made it all work.

Initial Setup on Media Temple ProCDN

This tutorial gives detailed instructions on how to set things up on the Media Temple side.

Here are some things to keep in mind –

1. Use HTTP Small objects for website CDNs.

We can create as many HTTP Small objects as we want. I created an object for each of my domains. Here is the more complete Edgecast HTTP Small Object guide.

2. The Directory Name must be unique for each HTTP object.

Since I am structuring my CDN according to domain, I named each directory based on domain. For example, suppose –

  • I have three domains –
    mydomain1.com, mydomain2.com, mydomain3.com
  • In this case, I would name my directories –
    mydomain1, mydomain2, mydomain3
  • My CDN links (non multisite) would look like this –
    http://wac.96cc.edgecastcdn.net/8096CC/mydomain1/wp-content/uploads/2012/12/myimage.jpg
    http://wac.96cc.edgecastcdn.net/8096CC/mydomain2/wp-content/uploads/2012/12/myimage.jpg
    http://wac.96cc.edgecastcdn.net/8096CC/mydomain3/wp-content/uploads/2012/12/myimage.jpg 
    

Setup on W3 Total Cache

This article describes how to setup w3tc with Media Temple’s ProCDN system.

In the configuration section –

  • Account # – This is the account number on the upper right of our Media Temple ProCDN control panel. This exact account number also appears in our full CDN link. If we desire, we may create a shorter, more pretty url by using cnames.
  • Token – This is like a password for accessing the ProCDN API. We can get our token by clicking on the My Settings link on the top right of our proCDN panel. This API and token are used when we purge files from our CDN.
  • SSL support – I set this to Disabled. In the ProCDN faq page, this is what Media Temple says –

    Do you offer SSL with ProCDN?
    Not yet. We know that this is important to our users and it’s on our roadmap.

  • Replace site hostname with – I enter my CDN path here. For example –
    wac.96cc.edgecastcdn.net/8096CC/mydomain1 
    

    Note – there is no http in the entry, just the main CDN domain and relevant path.

Translate Multisite Image Links

If we are running WordPress Multisite with directories, there is another thing we must address. On a multisite-directory setup, image links look something like this –

http://mydomain.com/myblog1/files/2012/12/myimage.jpg

These links then get translated in our .htaccess file to the actual URL which looks like this –

http://mydomain.com/wp-content/blogs.dir/2/files/2012/12/myimage.jpg

where 2 is the blog number for myblog1 in our multisite setup.

The problem is, we do not have the same ability to redirect links on our CDN. As a result, after CDN path replacement, our image links look like this –

http://wac.96cc.edgecastcdn.net/8096CC/mydomain1/myblog1/files/2012/12/myimage.jpg

This results in a file not found error because our image file actually resides in –

http://wac.96cc.edgecastcdn.net/8096CC/mydomain1/wp-content/blogs.dir/2/files/2012/12/myimage.jpg

One way to resolve this issue is to translate the links right in the w3tc plugin. After all, the plugin already extracts the relevant links and does CDN name replacements; we just need to add in our own directory redirect.

To do this –

  • I edit the file w3-total-cache/lib/W3/Plugin/Cdn.php in the w3tc plugin.
  • I look for the link_replace_callback function.
  • Within the function, I look for this line of code –
    $new_url = $cdn->format_url($path);     
    

    Right below it, I add the following –

    $new_url = apply_filters('shiba_cdn_remote_file_name', $new_url);    
    
  • Now, I can add in my own filter function to change my CDN urls. Below is an example filter function for translating my image urls.

// Add to the init function
add_filter('shiba_cdn_remote_file_name', 'my_cdn_check');

function my_cdn_check($remote_file) {
	if (strpos($remote_file, '/myblog1/files') !== FALSE) {
		$remote_file = str_replace('/myblog1/files', '/wp-content/blogs.dir/2/files', $remote_file);
	}
	return $remote_file;
}

In general, I do not like making changes to core plugin files, but I did not find a less intrusive alternative method. If you know of one, please let me know.

Make a copy of the original files before making any changes. In this way, we can revert back if there are any problems. Do not attempt to do this unless you are comfortable with ftp-ing files to and from your web server, and have access to your server file system.

Test CDN

Now we have everything set up!

It may take up to several hours for everything to propagate through. Before turning on the CDN option in w3tc, make sure that your images have propagated and are in the right place.

To test this, I enter some example CDN urls into my browser.

http://mydomain.com/myblog1/wp-content/blogs.dir/2/files/2012/12/myimage.jpg

If we choose to cache our css and js files, they will also take some time to propagate to our CDN.

Whenever there are updates to my css and js files, I make sure to purge all of my caches. I also purge my CDN cache by going to the Performance >> CDN screen, and clicking on the Purge button at the top. Then, I enter all the css and js files that I want to update.

I only turn on CDN caching for my css files when I am sure that there is a copy of it cached in my CDN. This ensures that my live pages will always be styled.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Comments

  1. Dan Baritchi says

    January 8, 2013 at 7:12 pm

    Hey ShibaSake!

    Re: “Translate Multisite Image Links” – I just ran into this same “file not found” problem with multisite upload file URL replacement – and this looks like an awesome solution!

    The only question I have: I notice your filter strpos and str_replace are hard-coding “myblog1” and the blog ID – is there a way to do this with a variable or function call in those 2 places instead (to determine the blog name and blog id) ?

    That would make this solution work for any multisite… LOL let me know if I’m mis-reading it too. πŸ™‚

    Thanks!
    Dan

    Reply
    • Dan Baritchi says

      January 8, 2013 at 7:17 pm

      have to add just saw the “dog” side of your site, and I love the pics of your pups, we’re huge dog lovers too. πŸ™‚

      Reply
    • Dan Baritchi says

      January 8, 2013 at 8:49 pm

      Actually here’s the solution I found:

      In strpos, check only for ‘/files/’
      Then replace ‘/files/’ with ‘/wp-content/blogs.dir/’ . $blog_details->blog_id . ‘/files/’
      And call this at beginning of your function: $blog_details = get_blog_details();

      Here’s the updated code block: http://pastie.org/private/l8agw417fxav2q5bp4hsw

      LOL feeling is mutual about editing plugin files, but this DOES work – excellent solution. πŸ™‚

      Have an awesome day!
      Dan

      Reply
      • ShibaShake says

        January 9, 2013 at 11:19 am

        Glad it worked out Dan, and thanks for posting your solution.

        I used the blogid because it is something that doesn’t change, so it saves a few steps (during page generation) to put in the constant values. Hugs to your furry gang! πŸ˜€

      • Dan Baritchi says

        April 24, 2013 at 10:36 am

        Hey there! just wanted to update you – the latest version of W3TC that came out a few days ago now does full CDN URL Replacement without needing the fixed above.
        When I first updated it yesterday, the CDN replacement URLs were all broken, then I realized my code above to “fix” it was what was breaking it – since it’s now fixed out of the box. πŸ™‚
        LOL love the husky close-up pic, that looks like one seriously sweet pup!!
        Have an awesome day!
        Dan

      • ShibaShake says

        April 24, 2013 at 6:26 pm

        Hey Dan,
        Thanks for the update. Good to know.

  2. Drew at (mt) Media Temple says

    December 4, 2012 at 3:21 pm

    Hey there ShibaShake! I really like the artwork on your site πŸ™‚

    Just wanted to thank you for the great article and thanks for mentioning (mt) Media Temple :). If you ever need anything from us over here, we’re available 24/7 through phone, chat and Twitter πŸ™‚

    Drew J
    (mt) Media Temple
    @MediaTemple

    Reply
    • ShibaShake says

      December 5, 2012 at 9:49 pm

      Thanks Drew. Happy Holidays!

      Reply

Recent Posts

  • Screenshot of an example article in code view of a modified Gutenberg editor.How to Harness the Power of WordPress Gutenberg Blocks and Combine It with Legacy Free-Form Text
  • Screenshot of the Success, WordPress has been installed page.Migrating Your WordPress Website to Amazon EC2 (AWS)
  • Screenshot of WinSCP for creating a SFTP configuration.How to Set-Up SFTP on Amazon EC2 (AWS)
  • WordPress Gutenberg code view screenshot of this article.How to Prevent Gutenberg Autop from Messing Up Your Code, Shortcodes, and Scripts
  • Screenshot of the Success, WordPress has been installed page.How to Create a WordPress Website on Amazon EC2 (AWS)

Recent Comments

  • Create Pop-up Windows in Your WordPress Blog with Thickbox (57)
    • Jim Camomile
      - I have used this tutorial several times and it is one of the few simple and effective ways to add popups with dynamic content, ...
  • How to Add Admin Columns in WordPress (7)
    • Andy Globe
      - Hi Friends, I am facing two problem in WordPress admin1. Load custom CSS for wp-admin on user-role base (editor) 2. ...
  • Example blog front-page using excerpts and the Shiba Theme.Optimize Your WordPress Plugins with Tags (5)
    • DF
      - thanks, i went the other way and added a filter if pages I wanted.
  • WordPress Search Widget – How to Style It (57)
    • Nelson
      - Tanks master - Fine
  • Update custom inputs with the proper data using Javascript.Expand the WordPress Quick Edit Menu (59)
    • Mike G
      - This is exactly what is happening to me. It is updating the value in the database and in the column, but I have to refresh ...

Copyright © 2022 · Genesis Skins by ShibaShake · Terms of Service · Privacy Policy ·