1
votes

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?

1
Because you're telling it not to cache and it must revalidate every time? This is possibly the most confusing thing I've ever read.animuson

1 Answers

2
votes

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.