You want to decrease page load time and eliminate unnecessary hits on your web server.
Employ caching mechanisms to indicate to browsers in that web pages, images, and other site elements can be displayed from locally saved versions, and that must be refreshed from a newer version on the server. With Apache's mod_expires module, you can control caching by file type and directory. PHP's header( ) function and HTML <meta> tags provide caching control for individual pages.
Cached files can speed the display of a web page. But lacking an expiration date for cached resources, a browser must still send a request to the server to check the validity of each file needed to render a page. Even when the server agrees that a file can be loaded from the cache, rather than downloading it again, the roundtrip between browser and server unnecessarily delays the page's load time and demands extra server processing cycles to complete. When the web server sends an explicit expiration time for each file, caching can eliminate the request-and-response routine the browser would otherwise need to validate its cache.
You need to restart Apache for any configuration change to take effect.
ExpiresActive On ExpiresByType image/gif "access plus 1 month" ExpiresByType image/jpg "access plus 1 month" ExpiresByType image/png "access plus 1 month"
The first line activates the Expires header for files sent from the directory. Then for each possible web site image type (GIFs, JPGs, and PNGs), the directive command access plus 1 month ensures that images will remain in the client's cache for a month after they were requested. Cache durations can be specified either from the current time (access) or from the file's last modified time (as in modification plus 1 month) in multiples of years, months, weeks, days, hours, minutes, and seconds.
Calls to the header( ) function must be placed at the top of your PHP scripts, before any other PHP code or the <html> element that marks the beginning of the visible part of the page. As used above, the header( ) function will output Expires: Sat, 16 Jul 2005 00:00:00 -0500 GMT. Only after midnight Central Time the following day will the page be refreshed from the browser and a new FAQ displayed.
Setting the expiration date to some time in the future will cause the page to be saved in the user's browser cache until she requests the page again after that date or manually clears her browser cache. In contrast with dynamic PHP pages, there's no easy way to constantly update a <meta> tag expiration date in a static page, so using a future date works best on pages that you know will remain unchanged for a long time.
Recipe 2.3 provides more information on where certain files on your site should go. For an overview of mod_expires, see http://httpd.apache.org/docs/mod/mod_expires.html.