Easy Cache Control With Apache Servers
When a browser gets a web page, it contacts the server and downloads all the pieces it needs to put together the page that you see. The pieces that are downloaded include the html text itself as well as any graphics, style sheets, javascript and so on. If a particular user opens the page again, all the items are downloaded again except those that are cached on the users device. If, for example, the user has to download a large graphic every time the page is loaded, the page will load slowly every time. If, however, the graphic is saved on the users device and is fetched from there, it, and the page, will load much more quickly.
Besides the user’s device, files may be cached on servers between your server and the users’s device. These proxy or gateway caches can also hold onto your files so that they can be retrieved before going on to the the user’s device. For more on cache management see: www.mnot.net/cache_docs/
To speed up delivery of the web page we want as many of the needed files as possible to be cached on the users device and, if not there, at least in the proxy and gateway caches. On the other hand we do not want to cache pieces that change frequently. To deal with this, we want to manage the way our files are cached. There are meta tags like:
<meta http-equiv="Cache-control" content="public"> <meta http-equiv="Cache-control" content="private"> <meta http-equiv="Cache-control" content="no-cache"> <meta http-equiv="Cache-control" content="no-store">
However, they may or may not work with a browser and they typically do not work with proxy caches. Instead, we want to specify the headers that are attached to the files when they are traveling through the internet. The easiest way to do that is by adding the following to your .htaccess file:
<IfModule mod_expires.c> ExpiresActive On Expires Default "access plus 1 seconds" ExpiresByType image/x-icon "access plus 30 days" ExpiresByType image/jpeg "access plus 30 days" ExpiresByType image/png "access plus 30 days" ExpiresByType image/gif "access plus 30 days" ExpiresByType application/x-shockwave-flash "access plus 30 days" ExpiresByType text/css "access plus 7 days" ExpiresByType text/js "access plus 7 days" ExpiresByType text/javascript "access plus 7 days" ExpiresByType application/javascript "access plus 7 days" ExpiresByType application/x-javascript "access plus 7 days" ExpiresByType text/html "access plus 30 minutes" ExpiresByType application/xhtml+xml "access plus 30 minutes" </IfModule>
You will want to change the durations to suit your needs.
Recent Comments