Serve static content from a cookieless domain
To improve web page performance it is good idea to serve static content from a cookieless domain.
What is Cookies less domain?
Domain which don’t require any cookies from client to serve the request is called cookie less domain. For example if we need to serve static images, there is no need to get or set any cookie, not even a session cookie.
Why not cookies?
Cookies normally are small amount of text stored on client. Cookies can be set for some domain or specific path in domain. If any cookies are set, browser will send these with each and every request made to display page, this includes all images, CSS, JS files and other static content or dynamic content.
For example if a page have 20 images and 1 CSS the browser will send 22 request to server. If cookies are of size 2kb, in total it will cause 44kb of network traffic for each page served. This will not only consume visitor bandwidth but also cause unnecessary delays for the visitor.
If we can serve images from a separate domain or possibly a sub domain with no cookies, we can save significant bandwidth and visitor’s time.
How to create cookie less domain?
To ensure this, when setting cookies make sure that you set cookies for particular domain. For example set cookies for www.mydomain.com, and server images from images.mydomain.com. If you set cookies for a subdomain or separate domain, they will not be available on other domains. So cookies set for www.mydomain.com will not be sent with all images called from images.mydomain.com.
PHP function setcookie allows to set cookies for particular domain. For example following code setcookie("TestCookie", $value, time()+3600, "/", "www.mydomain.com"); will set cookie which is only available for www.mydomain.com and not for images.mydomain.com. In contrast the code setcookie("TestCookie", $value, time()+3600, "/", ".mydomain.com"); will set cookies for all sub domains of mydomain.com.
Who is already using cookie less domains?
- Yahoo!: Servers images from yimg.com, a cookie less domain
- YouTube: Servers images from ytimg.com, a cookie less domain
- Amazon: Servers images from images-amazon.com, a cookie less domain
Yahoo.com! using yimg.com for images with no cookies. See request headers which don’t have any cookies being sent.

Another good idea for increased performance is to server static content from some light weight web server like lighttpd.
The funny thing using Google Page Speed is that Google told me that I should use Cookieless sub domain but all the cookies sent are Google Analytics, Google Adv… cookies
So the best web site optimization is just remove all Google products from the page!
If Google Analytics (GA) is used in page it will create few cookies. I think there is some option available in GA to limit the cookies to specific domain, you should try that. If you limit GA cookies to your main domain, they will not be sent with all images called.
Please where and how do i need to add the code in the post above? Is it function.php?
thank you, subscribed!
No, the code is just a sample. you need to find similar code/function in your site and make it use a specific domain, so other sub domains can become cookie less domains
how if we use like co.cc as free domain as cookieless domain, can we?
thanks
Yes any domain can be used, as image domain does not matter much.
Hi !
nice article and well explained .
I ran into lots of articles to serve static content from a cookieless domain , but this one seems to be the easiest for me to follow.
I am using vbulletin software , although I do not know where to find this PHP function , but it should be there some where.
now I am facing two issues here :
the first one is that I have to move files physically to their proper subdomain folders right ?
the second one is , I am wondering if I moved all images , css , java etc to a cookie-less sub domain , how do I overcome the issue of ( having posts around with the old images urls like ( http://www.mydomian.com/images/whatever.jpg ).
cause I don’t allow my community to upload images on any other server.
Thank you again for sharing such useful article .
If you are using linux machine you can overcome this issue by using soft links. In this way you images will be available from both (main and subdomain) so you new images will be moved to subdomain and old files will still accessible. Another method will be to rewrite old urls to new domain through .htaccess.
[...] Serving images from cookie less domain [...]
Hello,
I am trying the same for my website. So i created a static.mywebsite.com subdomain. I still see the message about cookieless domain in the “page speed” addon for mozzila.
Weird since i dont set any cookies on my website? I do use a session.
Commenting the session_start() in the beginning of my php script removes the “cookieless domain” warning. Un-commenting session_start() in my php script results in giving me the freakin warning again.
Seems like a session is setting cookies too??? I dont get it.
Can anyone explain this to me? And what should i do to fix it?
Greetings,
Marc
@marc
The message is there because session cookies is being set for *.mywebsite.com and not limited to http://www.mywebsite.com. If you don’t need sessions than keep
session_start()commented. At the moment i don’t know how to limit session cookie to a domain (as this cookie is not set from set_cookie() function), I think there must be some configuration to tell domain of the session cookie. Please try to set cookie “PHPSESSID” via set_cookie function and give domain param as http://www.mywebsite.com before the session_start, I am not sure if it will work but no harm in trying.hi,
i read through the whole article with lots of interest but lack of knowledge…
i run wordpress on two hosts, using c-names and a-records, with a total of 400 domain names
i run into the problem of file amount limitation, which is why i want to serve iamges from the main domain http://desatascosmadrid.biz
now, if i understood right, google and seo are not affected if e.g. i load images on articles on http://pocerosmadrid.biz using desatascosmadrid.biz/images/1.jpg or likewise…?!!
but, if understood, loading time due to cookie loading is high, so could/should i use a cookieless subdomain? and how do i set it up on wordpress if i’m only an end-user and have no access to server config?
sorry if my question is repetitive, i just couldn’t figure out what exactley would be applicable in my case
thanks in advance for any help
Yes you understood correctly, serving images from other domain will not be effected. I am assuming that desatascosmadrid.biz is also a good quality domain, if you use some domain which has been involved in spamming, or banned by google, it may effect.
For normal sites impact on loading time due to cookies is not high. Using different domain will already give parallel download for images etc. The images domain don’t always have to be cookieless. In your case the domain desatascosmadrid.biz is altogether a different domain, so cookies will not be sent with images.
I don’t know if and how the image url can be changed for the images in wordpress, but i am sure there must be a way. If possible we can change the url of images when adding images from media library.
I hope i answered your queries, thanks