19

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?

1
  • It appears the consensus is that while .htaccess could be used to redirect requests for www.example.com/images to static.example.com/images; the workload would decrease performance. The performance gain occurs only if the files are directly accessed from a cookieless domain in a single request. Another idea was to use a plugin that dynamically creates the references. Thank you all for the advise. Sep 17, 2009 at 12:33

7 Answers 7

15

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.

4

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.

3
  • Thanks for the idea GmonC, but may be beyond my skills. Will the large plugin library both Joomla and Wordpress have, I was mildly hoping I'd find something already along these lines. Sep 17, 2009 at 12:27
  • You can search the extensions directory in Joomla's official website. There's a plugin called "DirectPHP", you could write a 5 lines php script and call it from an article. But I agree that sometimes we just want an already working solution: since it's a cms, it should has some feature already implemented. Well, good luck with the other provided solutions!
    – GmonC
    Sep 17, 2009 at 12:59
  • ATG Dynamo is a commercial (and very expensive!) webapp framework which does this. You work with images in its CMS, and when you deploy content, it can push the images to a dedicated webserver, then generate the right URLs on the pages. It's a nice idea, and surely not all that hard to implement, so i'm surprised it's not more widely done. Jul 18, 2010 at 18:05
4

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.

3

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

1

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.

0

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.

0

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.

4
  • 1
    This doesn't do what the OP asked.
    – tiago
    Dec 13, 2012 at 5:32
  • Is there an equivalent of this for Nginx?
    – secondman
    Nov 1, 2015 at 11:20
  • Shows 503 for my website
    – Moxet Khan
    Feb 25, 2016 at 10:05
  • How set same value for Nginx?
    – himansu
    Sep 1, 2020 at 17:50

Not the answer you're looking for? Browse other questions tagged or ask your own question.