Webmasters Stack Exchange is a question and answer site for pro webmasters. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question

marked as duplicate by John Conde Jun 9 '12 at 17:36

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

Hmm, not sure why this was closed. It is similar but the OP is asking different questions. – DisgruntledGoat Jun 9 '12 at 17:42
up vote 1 down vote accepted

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.

share|improve this answer