304: Not modified and front end caching

I had this problem and it turned out to simply be that I had Firebug open. This has an option under the Net tab "Disable Browser Cache" that is ticked by default. There is a similar option in Chrome's developer tools, one of the tick boxes on the bar under the menu bar.

Unticking these options resulted in the browser correctly sending HTTP_IF_MODIFIED_SINCE and everything working fine after all (even with Firebug or Chrome Dev Tools open).


This article will answer all your questions on caching

I found that adding

RewriteRule .* - [E=HTTP_IF_MODIFIED_SINCE:%{HTTP:If-Modified-Since}]
RewriteRule .* - [E=HTTP_IF_NONE_MATCH:%{HTTP:If-None-Match}]

To the bottom of my htaccess file (below all rewriterule) worked.


HTTP_IF_MODIFIED_SINCE is the right way to do it. If you aren't getting it, check that Apache has mod_expires and mod_headers enabled and working properly. Borrowed from a comment on PHP.net:

$last_modified_time = filemtime($file); 
$etag = md5_file($file);
// always send headers
header("Last-Modified: ".gmdate("D, d M Y H:i:s", $last_modified_time)." GMT"); 
header("Etag: $etag"); 
// exit if not modified
if (@strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) == $last_modified_time || 
    @trim($_SERVER['HTTP_IF_NONE_MATCH']) == $etag) { 
    header("HTTP/1.1 304 Not Modified"); 
    exit; 
}

// output data