Recently, I started using Cnames for my CDN setup. This allows my static CDN content to have a pretty url that is tied to my own domain.
Here I discuss how I set-up a Cname for my Media Temple dv server and CloudFront CDN. Then, I highlight a loading issue I encountered with w3tc multisite and how I fixed it.
How to Setup a CDN Cname in Media Temple
IMPORTANT – Do not use the Plesk interface to set up a cname. Apparently, the Plesk DNS interface is only for instances where we are running our own local DNS server.
If we want to change DNS entries in the main Media Temple name server, then we need to use the Edit DNS Zone File option in our main Media Temple Console (NOT Plesk).
Here is the Media Temple tutorial on how to add or change a DNS record.
- Click on the Edit DNS Zone File option.
- Click the Add new record button.
- Enter our CDN subdomain in the name column, select CNAME from the drop down menu in the type column, and enter our CDN domain name in the data column. Note that there is an extra dot at the end of the domain name.
According to Media Temple it may take up to 24-48 hours for DNS propagation.
We can check our new DNS entry by using the dig linux command in ssh.
CNames and w3tc Multisite
Note that before our DNS record is fully propagated, our new sub-domain will *not* get properly redirected in WordPress multisite. This is because WordPress will try to interpret the sub-domain as a multisite blog.
In particular, if a file (e.g. minify file, image file) is not present in exactly the right location on my site server, it does not get automatically created. Instead, my blog front-page gets loaded. This is a problem because every time I purge my Minify cache, my minified css and js files no longer load properly.
Therefore, we only want to update w3tc *after* our new DNS record is fully propagated. We can determine why this occurs by looking at how minify and images files work in WordPress multisite and w3tc.
How minify works in w3tc –
- If a requested minified file is not there, a rule in wp-content/cache/minify/.htaccess redirects the request to wp-content/plugins/w3-total-cache/pub/minify.php
- minify.php in turn, starts WordPress by loading wp-load.php.
- Once that gets loaded, it generates the appropriate minify file.
How image files are redirected in WordPress multisite –
- If the image file is not present, our main domain .htaccess file redirects the request to wp-includes/ms-files.php.
- ms-files.php starts WordPress by calling wp-load.php, and then sends back the right headers and image file.
The problem arises when wp-loads.php calles the ms-settings.php file in a multisite configuration. In ms-settings.php, there is a check to ensure that the current site domain (e.g. shibashake.com) is equal to our HTTP_HOST (e.g. images.shibashake.com).
Before DNS propagation, my DNS Cname gets sent to my main server. As a result, the current site domain and HTTP_HOST no longer match (shibashake.com vs. images.shibashake.com). As a result ms-settings.php redirects to my main blog page. Here is the related code on ms-settings.php.
if ( $current_site->domain != $_SERVER[ 'HTTP_HOST' ] ) { header( 'Location: http://' . $current_site->domain . $current_site->path ); exit; }
To fix this, I add the following code into the wp-content/advanced-cache.php file as well as the w3-total-cache/wp-content/advanced-cache.php file.
if ($_SERVER[ 'HTTP_HOST' ] == 'images.shibashake.com') $_SERVER[ 'HTTP_HOST' ] = 'shibashake.com';
In this way, there are no redirects, and my minify file creation issues and image file redirect issues are fixed.
How to manually create a minify file
The code fragment below allows us to manually call w3tc to generate a minify file. It comes from the w3-total-cache/w3/Plugin/CDNAdmin.php file (line 279).
$local_file_name = 'wp-content/cache/minify/000001/abcde.css'; $dispatcher = w3_instance('W3_Dispatcher'); if (!file_exists($local_file_name)) { $dispatcher->create_file_for_cdn($local_file_name); }
Note that the css id for $local_file_name must match with the current id used by the w3tc plugin. This allows us to manually create all relevant minified files whenever we want to.
zisses says
I have used your widget for years on my sites because of the unique and easy configuration ability it provides and the overall site appearance after things are in their right place.
This morning while working on a site I noticed I had not yet downloaded your widget and couldn’t find it in the wordpress plugin add new search. So I visited you site to find the download. Today unexplained for the first time I paid attention to the art and detail you have built into your wordpress website and it’s amazing.
I have a decent handle on making minor corrections and changes and can deal with widgets and basic site setup but I need someone with your knowledge to help me with design and structure so I can have sites that look professional and convert to continue my dream of an online business. If you feel you have time and are willing to provide me with some excellent design and implementation and of course If I can afford your services will you please contact me anytime is fine but I am on PST. I truly hope you say yes because It’s so important to my success.
ShibaShake says
Thank you very much for your wonderful comment.
In terms of site design, most of the stuff that I use is freely available. My current child theme, Genesis Skins, is available here. It is built on top of the Genesis Framework though, so it will need that. I am a big fan of Genesis because it offers a clean layout and is mobile responsive.
I currently only do WordPress for fun and in my spare time. 😀