9.3. Improving Site Performance

Problem

You want to decrease page load time and eliminate unnecessary hits on your web server.

Solution

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.

Discussion

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.

Apache's mod_expires module sets an expiration date for a resource and sends it to the browser in the Expires http response header for each request. The Expires header is a date and time after which a site file should not be cached. If you have access to your Apache configuration file (typically, httpd.conf) you can check to see if mod_expires ...

Get Web Site Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.