Possible Duplicate:
.htaccess and browser caching

There are a lot of snippets that enable caching on a website and I don't know which one should I use. The most popular is something like this:

<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access 1 year"
ExpiresByType image/png "access 1 year"
ExpiresByType text/css "access 1 month"
ExpiresDefault "access 2 days"

I also found something similar, but with keyword 'plus'. Like this:

ExpiresByType image/png "access plus 2592000 seconds"

What does it mean, because I didn't find anything in the documentation.

Another snippet I found:

<ifModule mod_headers.c>
  <filesMatch "\.(ico|jpe?g|png|gif|swf)$">
    Header set Cache-Control "max-age=2592000, public"
  <filesMatch "\.(css)$">
    Header set Cache-Control "max-age=604800, public"
  <filesMatch "\.(js)$">
    Header set Cache-Control "max-age=216000, private"
  <filesMatch "\.(x?html?|php)$">
    Header set Cache-Control "max-age=600, private, must-revalidate"

What is the best practice?

  • Hmm, not sure why this was closed. It is similar but the OP is asking different questions. Jun 9 '12 at 17:42

Regarding the first snippet, you should avoid using the ExpiresDefault line as that will cache all your pages as well which you probably don't want.

The 'plus' keyword you mention is optional, see the Apache docs for details. So that snippet is the same as the first but with the expiry time in seconds, not days.

The final snippet is a different caching method, but which basically has the same result. Some resources suggest Cache-control is better than Expires, I think because it plays more friendly with proxies. I'm not sure why the line for Javascript sets it to private since that is equally as valid set to public.