How to set up a cookieless domain

cookieless

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
</FilesMatch>
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', 'YOUR-DOMAIN-NAME.com' );

3 Comments on “How to set up a cookieless domain

  1. John says:

    Kudos!
    I eliminated very stubborn cookies from my site thanks to you.
    The input to htaccess didn’t do it but with the define cookies in the config file made it happen.
    Every suggestion prior to this didn’t work because it was incomplete.
    You’ve got the brains. You’ve got the right stuff.
    Thanks again,

  2. nagaraj says:

    getting “internel server error” after add this code below of

    RewriteEngine on
    RewriteRule ^([a-z0-9_-]+)\.html$ index.php/page/$1 [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond $1 !^(index\.php|asset|robots\.txt)
    RewriteRule ^(.*)$ index.php/$1 [L]

  3. Xpark Media says:

    @nagaraj,

    The problem may be in your code, is there any reason you are modifying the standard WP rewrite rules?


Leave a Reply

Your email address will not be published. Required fields are marked *