StackOverflowNewbie
StackOverflowNewbie

Reputation: 40633

Last-Modified not working for .htaccess

I'm tyring to implement browser caching and follow Google PageSpeed's recommendation about setting Last-Modified to a data that is "sufficiently far enough in the past." I have the following in my .htaccess:

<IfModule mod_headers.c>
 <FilesMatch "\.(json|pdf|swf|bmp|gif|jpeg|jpg|png|svg|tiff|ico|flv|js)$">
  Header Set Last-Modified "Fri, 01 Jan 2010 12:00:00 GMT"
 </FilesMatch>
</IfModule>

I have mod_headers installed on my server.

Unfortunately, Google PageSpeed still complains and warns me:

Leverage browser caching

The following cacheable resources have a short freshness lifetime. Specify an expiration at least one week in the future for the following resources:

And then lists PNGs, GIFs, JPGs, etc. Yahoo YSlow says basically the same thing.

Looking at the response headers of one of my resources that should be caching, I see this:

Date:           Tue, 19 Oct 2010 20:12:04 GMT
Server:         Apache/2.2.14 (Ubuntu)
Last-Modified:  Tue, 07 Sep 2010 23:51:33 GMT
Etag:           "2e0e34-2a43-48fb413a96a20"
Accept-Ranges:  bytes
Content-Length: 10819
Content-Type:   image/png

As you can see, the Last-Modified data does not match what I specified in .htaccess.

Any ideas what I am doing wrong?

Upvotes: 3

Views: 15740

Answers (3)

Lynn
Lynn

Reputation: 1

This works:

<IfModule mod_expires.c>
# Enable expirations
ExpiresActive On 
# Default directive
ExpiresDefault "access plus 1 month"
# My favicon
ExpiresByType image/x-icon "access plus 1 year"
# Images
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType image/jpg "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 month"
# CSS
ExpiresByType text/css "access 1 month"
# Javascript
ExpiresByType application/javascript "access plus 1 year"
</IfModule>

Upvotes: 0

Marco Demaio
Marco Demaio

Reputation: 34397

Removing Last-Modified is not what Google PageSpeed is asking. It wants to see the following headers in your servers response when browsers asks for static files:

Cache-Control max-age=...
Expires ...

in place of dots the server will place values.

In order to do this, you simply need to add to .htaccess the following lines:

<IfModule mod_headers.c>
 <FilesMatch "\.(json|pdf|swf|bmp|gif|jpeg|jpg|png|svg|tiff|ico|flv|js)$">
  ExpiresActive On
  ExpiresDefault "access plus 1 year"
  Header append Cache-Control "public"
 </FilesMatch>
</IfModule>

You'll see Google PageSpeed stopping to complain.

Upvotes: 7

warandpeace
warandpeace

Reputation: 281

Have you considered just using unset Last-Modified?

Example:

<IfModule mod_headers.c>
 <FilesMatch "\.(json|pdf|swf|bmp|gif|jpeg|jpg|png|svg|tiff|ico|flv|js)$">
  Header unset Last-Modified
 </FilesMatch>
</IfModule>

The FilesMatch section looks fine, so it's probably just some fiddly bit with Header Set. Hell, might even be case sensitive. Try Header set instead of Header Set

If this isn't what you want, then let me know and I'll think about it a bit more. Unset should work though,

Upvotes: 2

Related Questions