.htaccess: How to "Specify a cache validator"?

StackOverflowNewbie picture StackOverflowNewbie · Sep 4, 2010 · Viewed 84.9k times · Source

I'm running Google PageSpeed on my site and it's tell me that I need to
"Specify a cache validator."

The following resources are missing a cache validator. Resources that do not specify a cache validator cannot be refreshed efficiently. Specify a Last-Modified or ETag header to enable cache validation for the following resources:

... then it lists images, CSS, JS, etc.

According to http://code.google.com/speed/page-speed/docs/caching.html#LeverageBrowserCaching:

Set the Last-Modified date to the last time the resource was changed. If the Last-Modified date is sufficiently far enough in the past, chances are the browser won't refetch it.

I have the following in my .htaccess:

<IfModule mod_headers.c>
    <FilesMatch "\.(bmp|css|flv|gif|ico|jpg|jpeg|js|pdf|png|svg|swf|tif|tiff)$">
        Header set Last-Modified "Tue, 31 Aug 2010 00:00:00 GMT"
    </FilesMatch>
</IfModule>

What am I doing wrong?

Answer

aularon picture aularon · Sep 4, 2010

I think the problem you are having is with Expire: and not with Last-Modified:. Apache would by default send the file Last-Modified: header based on the file date. I suggest removing the upper code and replacing it with the following:

<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresDefault "access plus 1 year"
</IfModule>

Try with that, if it didn't work try adding this as well:

<IfModule mod_headers.c>
    <FilesMatch "\.(bmp|css|flv|gif|ico|jpg|jpeg|js|pdf|png|svg|swf|tif|tiff)$">
        Header set Last-Modified "Mon, 31 Aug 2009 00:00:00 GMT"
    </FilesMatch>
</IfModule>