Question

One of YSlow's measurables is to use cookie-free domains to serve static files.

"When the browser requests a static image and sends cookies with the request, the server ignores the cookies. These cookies are unnecessary network traffic. To workaround this problem, make sure that static components are requested with cookie-free requests by creating a subdomain and hosting them there." -- Yahoo YSlow

I interpret this to mean that I could experience performance gains if I move www.example.com/images to static.example.com/images.

Although this is easy to do, I would lose the handy ability within my content management system (Joomla/WordPress) to easily reference and link to these images.

Is it possible to use .htaccess to redirect all requests for a particular folder on www.example.com to a folder on static.example.com instead? Would this method also fool the CMS into thinking the images were located in the default locations on its own domain?

Was it helpful?

Solution

Is it possible to use .htaccess to redirect all requests for a particular folder on www.example.com to a folder on static.example.com instead?

Possible, but counter productive — the client would have to make an HTTP request, get the redirect response, then make another HTTP request.

This costs a lot more than the single line of cookie data saved!

Would this method also fool the CMS into thinking the images were located in the default locations on its own domain?

No.

OTHER TIPS

Although this is easy to do, I would lose the handy ability within my content management system (Joomla/WordPress) to easily reference and link to these images.

What you could try to do is create a plugin in Joomla that dinamically creates these references.

For example, you have a plugin that when you enter {dinamic_path path} in an article, it appends 'static.example.com/images' to the path provided. So, everytime you need to change the server path, you just change in the plugin. For the links that are already in the database, you can try to use phpMyAdmin to change them in this structure.

It still loses the WYSIWYG hability in TinyMCE, but is an alternative.

In theory you could create a virtual domain that points directly to the images folder, such as images.example.com. Then in your CMS (hopefully at the theme layer) you could replace any paths that point to the images folder with an absolute path to the subdomain.

The redirects would cause far more network traffic, and far more latency, than simply leaving things as they are.

It would redirect the request but the client would still be sending its cookies to the server, so really you accomplished nothing. You would have to directly access the files from a domain that isn't storing cookies for it to work.

What you really want to do is use staticexample.com/images instead of static.example.com/images so that you don't pick up any cookies on the example.com domain that you may have set. If all you do is server images from that domain with a simple apache server or something then you can configure that server not to return even a session cookie.

The redirects are a very bad idea. Cookies cause some performance hits but round trips to the server such as a redirect would cause are a much more serious performance issue.

I did below and gained success:

<FilesMatch "!\.(gif|jpe?g|png)$">
    php_value session.cookie_domain example.com
</FilesMatch>

What it means is that if you do not set images in cookie information. Then images are cookie-free with server.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top