views:

24

answers:

1

Our website was just converted to being generated by mod_rewrite and php scripts. Images aren't caching in browsers when they seemingly should be.

All images follow format:

<img src="/images/header.png" />

I must avoid the script completely caching because the PHP parser needs to handle each page dynamically on each request; however, the download overhead of the large images is cumbersome on every single page load.

I would ideally provide headers for "Cache-Control: no-cache, must-revalidate" and "Expires: some_date_in_the_past" to force revalidation of the PHP script.

Why isn't the browser caching static images with consistent href values across all pages?

+2  A: 

You can tell in your .htacess file what file extensions you want to apply certain headers on so for static content you could provide a far future expires header and for dynamic content you may wish to write cache-control no cache must revalidate.

<IfModule mod_deflate.c>
<FilesMatch "\.(js|css|htm|php)$">
SetOutputFilter DEFLATE
</FilesMatch>
</IfModule>

<IfModule mod_expires.c>
    <FilesMatch "\.(gif|jpg|jpeg|png|swf|css|js|html?|xml|txt)$">
        ExpiresActive On
        ExpiresDefault "access plus 10 years"
        ExpiresByType image/x-icon A604800
        Header set Cache-Control "max-age=290304000, public"
    </FilesMatch>
</IfModule>

Like in this example i have added a far future expires header to all different static file matches.

Note the FileMatch tag and the file extensions. The .htacess file is kept in any folder inside your application and it gives that folder these settings.

sushil bharwani
Looks like the headers are being sent properly with the new .htaccess file, but the browser is still requesting the image on each page load.http://getsoundaround.com/images/icons/icon_email.pngGET /images/icons/icon_email.png HTTP/1.1HTTP/1.1 200 OKDate: Mon, 14 Jun 2010 21:04:14 GMTServer: Apache/2.2.12 (Ubuntu)Last-Modified: Tue, 16 Dec 2008 11:52:18 GMTEtag: "3602e-1f8-45e28975d0080"Accept-Ranges: bytesContent-Length: 504Cache-Control: max-age=290304000, publicExpires: Thu, 11 Jun 2020 21:04:14 GMTConnection: closeContent-Type: image/png
scootklein