How to set up a cookieless domain

In this post we are going to explain how to set up a cookieless domain. In other terms we are going to remove the cookies set by the server/browser on each request for static content.

First let’s talk about what is static content. These are files that does not change on every requests, files like images, css, js, txt, videos, pdfs. In other words files that are not use to build dynamic content like php, aps(x), jsp.

Why do we want to remove the cookies from static content? you may ask, this reduces the total size of requests made to build the web pages and makes them load faster.

Removing the cookies from the request header is one of the performance settings needed to speed up your site. Remember that you still need to cache and compress some files.

The code

In the root folder of your site(s) add a .htaccess file and add the following code, as as many file extensions as you need. Note that this method will only work on unix/apache servers.

<FilesMatch "\.(js|css|jpg|png|jpeg|gif|xml|json|txt|pdf|mov|avi|otf|woff|ico|swf)$">
	RequestHeader unset Cookie
	Header unset Cookie
  	Header unset Set-Cookie
Additional options

Here are other settings that you can add to the .htaccess file that can help reduce the request header even more. Also you may want to use a subdomain (cname) to load all your static content.

Header unset Pragma
FileETag None 
Header unset ETag

For WordPress

In WordPress you also want to set (restrict) cookies to one domain by adding the following code to your wp-config.php file.

define( 'COOKIE_DOMAIN', '' );