Categories
How To

Cookie Free Domains and CDNs

Messing with static locations for static things on a website.

After I dropped Cloudflare I went back looking at other ways to speed up my site and what could I optimize things. As I mentioned in Static Content Subdomain, one of the benefits of that mess was to allow me to have a cookie free domain and, in having a cookie free domain, I basically made a domain that could be used as a CDN for my (mostly) static files.

It was noted then that, if you’re a non-www person, you have to actually use a separate domain. I do. I hate www in URLs. So I picked up a free domain for the site where I’m doing all this: ex-static.com. I probably could have gotten it even shorter if I’d tried sitecdn.net but that was short enough for me.

Since I already had everything over at /home/user/public_html/static I created an add-on domain in cPanel and told it to use that as its home.

Then I ran a search/replace on WordPress:
wp search-replace static.example.com/wordpress ex-static.com/wp-content/uploads
wp option set upload_path /home/example/public_html/static/wp-content/uploads

Since I’d already changed my media settings, that was all I needed to really run a replace. It changed my media settings too! Since I was planning to move my static content, not just images, I changed the path too, so I had to go back and change upload_path as well, but wp-cli is my friend.

Next up, because of the static content move, I copied my wp-content folder over and added this to my wp-config.php:

define('WP_CONTENT_DIR', $_SERVER['DOCUMENT_ROOT'] . '/static/wp-content');
define('WP_CONTENT_URL', 'http://ex-static.com/wp-content');

Boom. Everything’s where I want it.

Sadly, it got messy for MediaWiki and ZenPhoto. The later doesn’t allow you to move your themes folder at all so I made a symlink to /home/example/public_html/static/themes/themename/ and called that a day. MediaWiki was a damn dirty lie.

Supposedly you can set this:

$wgStylePath        = "http://ex-static.com/wiki/skins";
$wgStyleDirectory   = "/home/example/public_html/static/wiki/skins";

As soon as I did that, I got errors all over the place and had to change the directory path back:

$wgStyleDirectory   = "$IP/skins";

Weirdly named, $IP is set as follows:

$IP = "/home/example/public_html/wiki";

I did the same thing as with ZenPhoto and made a symlink then I could change the style directory.

Lastly (and this was last for a reason) I changed my .htaccess rule:

# CDN
<If "%{HTTP_HOST} == 'example.com' ">
        RedirectMatch ^/static/(.*)$ http://ex-static.com/$1
</If>

This was last so that I could not break things mid-flight. And I added in this:

# CDN
<If "%{HTTP_HOST} == 'static.example.com' ">
        RedirectMatch ^(.*)$ http://ex-static.com/$1
</If>

Everything works as expected. And? I have cookie free domains and I’m ready to move all my data off to a CDN whenever I’m ready for that step. The CDN would only have to cover the ex-static.com domain, to boot!

One reply on “Cookie Free Domains and CDNs”

looking this over I am a bit confused on what domain and what gets changed. I have a simple domain I got today for everything. so I would like to set this up if it works, which I am sure it does. I am just unsure of this domain gets what code.

Comments are closed.

%d bloggers like this: