I’ve been doing a lot of work on BookScouter.com lately to reduce page load time and generally increase the performance of the website for both users and bots. One of the tips that the load time analyzer points out is to enable an expiration time for static content. That is easy enough for images and such by using an Apache directive such as:
ExpiresActive On ExpiresByType image/gif A2592000 ExpiresByType image/jpg A2592000 ExpiresByType image/png A2592000
But pages generated with PHP by default have the Pragma: no-cache header set, so that the users’ browsers do not cache the content at all. In most cases, even hitting the back button will generate another request to the server which must be completely processed by the script. You may be able to cache some of the most intensive operations inside your script, but this solution will eliminate that request completely. Simply add this code to the top of any page that contains semi-static content. It effectively sets the page expiration time to one hour in the future. So if a visitor hits the same URL within that hour, the page is served locally from their browser cache instead of making a trip to the server. It also sends an HTTP 304 (Not Modified) response code if the user requests to reload the page within the specified time. That may or may-not be desired based on your site.
$expire_time = 60*60; // One Hour header('Expires: '.gmdate('D, d M Y H:i:s \G\M\T', time() + $expire_time)); header("Cache-Control: max-age={$expire_time}"); header('Last-Modified: '.gmdate('D, d M Y H:i:s \G\M\T', time())); header('Pragma: public'); if ((!empty($_SERVER['HTTP_IF_MODIFIED_SINCE'])) && (time() - strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) < = $expire_time)) { header('HTTP/1.1 304 Not Modified'); exit; }