Mike Rogers on a Snowmobile

Using cookieless domains to improve a website performance

Posted on

Fast loading times are a really important factor when it comes to website ranking, so it's important to remove as much unnecessary data as possible. A good method to do this is via cookieless domains.

Cookieless domains are (as the name suggests) are domains, in which the user will not send cookies (Which can add quite a few kilobytes to a request). For example, say I want a user to load a static image it would be silly of them to also send me the cookie data. Luckily they are super easy to set up.

Set up a sub-domain

Firstly, set up a subdomain on your website. For example static.yourdomain.com would be suitable, but it's really up to you. Now point the domain to a path where you will keep your static files (Such as images and CSS files). For Full On Design, I set up files.fullondesign.co.uk.

Stop cookies being set site wide

Next, make sure your website does not set website wide cookies. The fastest way to do this is by amending the .htaccess file and adding a CookieDomain Directive. For example:

CookieDomain www.fullondesign.co.uk
View on Gist Github

Alternatively in PHP's setcookie() function, I could just use something like:

setcookie("TestCookie", 'Some Value', time()+3600, "/", "www.fullondesign.co.uk", 1);
View on Gist Github

The key point to note is that the domain parameter is set to www.fullondesign.co.uk, not .fullondesign.co.uk. Thus the cookies will only be sent when the subdomain is www.fullondesign.co.uk.

Now start linking up to files in that directory and your pretty much done.

Setting up cookieless domains in Wordpress

Wordpress has a few neat built in functions to make this process easier. If you use wordpress pop the following code (just before the "That's all, stop editing! Happy blogging." comment) into your config.php file (amend as required):

define('COOKIE_DOMAIN', 'www.fullondesign.co.uk');
View on Gist Github

You can also change the uploads URL of your Wordpress install to point to your cookieless domain. In the config.php file add the following(amend as required).:

define('WP_CONTENT_URL', 'http://files.fullondesign.co.uk');
View on Gist Github

Useful Resources

I've barely touched on the topic of cookieless domains aside from just saying "They making your website load faster", here a two website which go a little more in depth.

This was written by Mike Rogers, a freelance Ruby on Rails developer based in London.

Share the ♥ by sharing this!

If you want to discuss this post, feel free to tweet me (@MikeRogers0) or drop me an email. Any code samples unless stated otherwise are licensed under the The MIT License (MIT). Spotted a mistake? Send me a pull request :)