WordPress Cookieless Domains

First off, cookieless domains are your friends for content distribution. Why? Because every time a web browser talks to a server it sends a copy of all its cookies for that domain. As you can imagine this is quite ridiculous if your client is requesting an image, CSS, JS or other static resource which can never use that cookie information. These wasted upload bits are totally unnecessary so lets get rid of them with the help of a cookieless domain.

Sub or New Domain?

You might think there is a magical ‘no cookies’ option on your server but there isn’t. A cookieless domain is simply one that has never set a cookie. This means two options; create a sub domain (like s.forstmist.org) or create a new domain name.

At first glance a sub domain seems nice and simple but there is a caveat. If you redirect your visitors from a sub domain (www.forestmist.org) to your root domain (forestmist.org) you’ll end up with cookies on your root domain. These root cookies are automatically sent along with any requests to all sub domains. That’s right, having cookies on your root domain taints all your sub domains from becoming cookieless domains. This is not a concern if you channel all your visitors to a www sub domain since any cookies there will not be available to the root domain and therefore won’t taint other sub domains.

For short domain name lovers, registering a new domain for serving static content is a no brainer. I definitely fall into this category so I registered forestmisty.org which I shall use as an example in the rest of this guide.

.htaccess Redirect

With the new misty domain setup and pointing to my web server everything was working great. Requests to static images weren’t passing cookie information but what if someone went to the root of forestmisty.org? What if a broken link sent them to a 404? WordPress would handle the request and that means potential cookies from it or Google Analytics. Yikes!

What would be nice is if we can allow any requests for forestmisty.org/wp-content/ but redirect anything else back to forestmist.org. We can do exactly that with the following .htaccess commands.

RewriteEngine on
RewriteCond %{HTTP_HOST} ^forestmisty.org$ [NC]
RewriteCond %{REQUEST_URI} !^/wp-content/?(.*)$ [NC]
RewriteRule ^(.*)?$ http://forestmist.org/$1 [R=301,L]

The first line enables the Apache rewrite engine.
The second line says anytime a request comes in for ‘forestmisty.org’ continue to the next line.
The third line says if the request is NOT for ‘/wp-content/…’ continue to the next line.
The forth line says if we made it this far redirect the original request to forestmist.org including any extra query information like /favicon.ico, /creative-resume, etc…

By the way…
^ means the start of a string.
$ means the end of a string.
? means the preceding character or collection in parenthesis is optional.
. means any one character.
* means the preceding character can occur 0 or more times.
$1 means insert whatever was inside the first set of parenthesis to the left.
[NC] means not case sensitive.
[R=301] means do a 301 redirect.
[L] means the last rule so don’t bother processing the rest of the .htaccess file.

Now we can rest assured that requests to our new cookieless domain will stay cookie free by only servering up what are almost always static file requests from the /wp-content folder.

Configure WordPress

Our new domain is setup and protected pretty well thanks to our .htaccess file so now it’s time for WordPress to join the fray. Specially, WordPress needs to have any /wp-content  links rewritten to point to the same location on our cookie free domain.

  • Download, install and activate the excellent CDN Linker by W-Mark Kubacki.
  • Login to WordPress
    • Settings » CDN Linker
      • Enter your new cookieless domain for CDN URL.
      • Change your ‘Include Dirs’ line to read ‘wp-content’ if you setup your .htaccess file like mine.
      • Save Changes

Load your home page and view source. You should see your new cookieless domain being referenced quite often. Yay!

Success

With a little bit of effort our server is less stressed, our clients are loading faster and hopefully you’ve got a silly grin on your face.


Freedom

One of my daily destinations is a guiding web page called New Life. A very simple design containing five key concepts integral to my new self. These are Sleep, Fancy Dresser, Grooming, Focus and Freedom.

Of all these, freedom, the most etherial has shown itself of late. For brief interludes yes but a perfumed breeze of moisture is far more than sufficient for a fremen. Invigorated and renewed I find it easy to continue the journey knowing that no matter what happens, I chose the right path.

Much of these feelings are thanks to my new freelancing lifestyle. I choose what I work on, who I work with and have no obligations as a provider until I choose to accept them. Thanks to careful selecting, everything I work on is a tremendous learning font. Each hand crafted creation empowering me with abilities needed for my lifetime goals. No effort wasted, a perfect journey!

When I was a child freedom meant my huffy bicycle. Now that I am older, more nuanced and most importantly, know myself, freedom is so much more. Still, a simple picture of open skies, a classic muscle car and an airport runway evoke everything I need to ascend into the feeling of freedom.

Freedom must be different for each individual but for me, I know what it is, I have tasted it and I will work for it. When it arrives, we’ll go for a drive together. Two old friends reunited at last and with no guilt for the past.

The open road calls. Won’t you join me?


Resignation from Mercy

Resignations seem like one of those taboo subjects that people shy away from so here is one I wrote today.

Very much in my own personal style and flavoured with naval metaphors. Yum!

Letter of Resignation

This letter is official notice of my resignation. My last day will be July 26th, 2011.

I came to Mercy as part of an elite team of handpicked individuals under the leadership of the great Fernando Martinez. The goal, to turn Mercy around and become a shining beacon of the latest technology.  It worked too. We had virtual servers, full system encryption, intrusion protection and more years before anyone else. A golden age of tech.

Rough times were coming though. Economic hardships, budget cuts, management changes and other hardships. Fernando and the team saw that they would be forced to make compromises if they stayed so they rightly moved on to new projects and challenges outside of Mercy. A few of us stayed though. For very different reasons I’m sure.

For me, the mission wasn’t complete yet. I would be crafty, work harder than ever before,  use open source software and do everything in my power to keep Mercy cutting edge. It didn’t work out quite as planned. Information Systems lost people and resources while gaining more responsibility. Soon it was all that we could do but tend to a once beautiful ship, tossed and broken, lost in the high seas with no shore in sight.

Fast forward to today and the ship is docked in the kingdom of the HCA. She is being patched, has new crew and officers getting ready to sail to new lands.

My modified quest is now complete and I’m very proud of my time here. I’m especially fond of all the smart people I’ve worked, played and lived with. You shall be remembered.

Soon I shall be off to new Freelancing adventures and doing what I love the most. Working with technology and making people happy.

May the wind always be at your back and your crew by your side!

^_^

Daniel Gagan
PS – Fun fact, I was born on July 26th at Mercy, hired years later on the same day and now the circle will be complete as my last day will be that very special date.