What is a cookieless domain?
How to set up a cookieless domain
In order to set up a cookiesless domain a couple of steps need to be taken.
- Register a new domain name. This can be a root domain or subdomain. I prefer to register the subdomain ‘static’ for the website in question. So for example: http://static.banihani.net is my cookieless domain.
- Configure your DNS database with a CNAME record to your root domain. On this website that would be: http://www.banihani.net.
- Configure your web server to serve static content without cookies. This can be done through an .htaccess file.
1. Register a new domain name
In order to set up a cookieless domain you need a different domain name. This can be either a subdomain (static.wesleysmits.com) or a root domain(wsmits.com). In most cases no problems will occur when using a subdomain. Since this is free most of the time this is a better alternative for most people.
If you use your root domain instead of the www-version (example.com instead of www.example.com) then using a sub-domain as a cookieless domain won’t work. In this case you would need to use another root domain as a cookieless domain. Google for example uses http://gstatic.com as a cookieless domain.
Note: If you are using Google Adsense on your website this issue will also occur because Google Adsense sets cookies on the root domain and all sub-domains. This behaviour can not be overrideen.
2. Configure your DNS settings
You should set an A record from your cookieless domain to your website domain. This goes for both alternatives (Subdomain and Root domain). The recommendation on most tutorials i’ve found is to use a CNAME record. While this does the job it’s less effective because the DNS resolution of a CNAME takes another route of indirection by finding the domain that it’s being pointed to and then resolving that.
3. Configuring the web server to serve static content
Configuring your web server to serve static content without cookies is done with a faily simple piece of code that should be placed in the .htaccess file in the root of your domain.
Code snippet credit should go to K. Worthington
# Use Mod_deflate to compress static files <filesmatch ".(js|css|ico|txt|htm|html|php)$"=""> SetOutputFilter DEFLATE # Speed up caching FileETag MTime Size # Expires ExpiresActive On ExpiresDefault "access plus 366 days" # Future Expires Headers <filesmatch ".(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf)$"=""> Header set Expires "Sat, 27 Dec 2014 23:59:59 GMT"
Once you’ve done that just configure your actual domain to point all the links to retrieve the files to the static domain.
For example: Usually i might retrieve my CSS file like this:
<link rel="stylesheet" type="text/css" href="/css/main.css">
Now it would look like this.
<link rel="stylesheet" type="text/css" href="http://statics.banihani.net/css/main.css">