Knowledgebase
How to improve caching with mod_expires
Mod_expires is an Apache module that is used to manipulate the Expires HTTP header and the max-age directive of the Cache-Control header. These two HTTP headers instruct the internet browser of the client for how long should particular documents and files be cached. This cache is very effective because it's on the side of the client. The cached files are stored on the local computer of the client which means that they don't have to be transferred from the server for as long as they are considered valid (until the cache expires).
All HostKnox servers have mod_expires installed. To enable it and use it you need to put some directives in an .htaccess file in the public_html folder on your hosting account. The directives work recursevily which means that they will be applied to files that are in any of the subfolders of public_html. Of course, if you want the rules to apply to files only in certain subfolders, you can put the directives only in .htaccess files in the particular subfolder(s).
You can create an .htaccess file from the Files section of the Pixie control panel; use the Create File button that's on the right side. If there's an .htaccess file in the particular folder (e.g. public_html), simply edit. Put the directives on a new line and save the file.
You can use various directives depending on which file types you want to be cached and for how long. For example, the directive:
<IfModule mod_expires.c> ExpiresActive On ExpiresDefault "access plus 2 months" </IfModule>will instruct the browser to cache all files for two months. You can list the caching time in months, weeks, days, hours, etc.
You can specify the caching time for particular file types by using the ExpiresByType directive. For example, with the following rules:
<IfModule mod_expires.c> ExpiresActive On ExpiresDefault "access plus 2 days" ExpiresByType image/jpeg "access plus 2 months" ExpiresByType image/gif "access plus 2 months" ExpiresByType image/png "access plus 2 months" </IfModule>JPEG, GIF and PNG images will be cached for two months and all the rest of the files will be cached for two days.
You can use the FilesMatch directive to exclude particular file types from being cached. For example, with the following directives:
<IfModule mod_expires.c> ExpiresActive On ExpiresDefault "access plus 2 days" ExpiresByType image/jpeg "access plus 2 months" ExpiresByType image/gif "access plus 2 months" ExpiresByType image/png "access plus 2 months" <FilesMatch "\.(php|cgi)$"> ExpiresActive Off </FilesMatch> </IfModule>JPEG, GIF and PNG images will be cached for two months. The rest of the files will be cached for two days with the exception of PHP and CGI files which won't be cached at all.
Of course, if you don't use the ExpiresDefault directive, it won't be necessary to exclude file types at all; you can only use the ExpiresByType directive to list only specific file types that are to be cached. For example, with the following rules:
<IfModule mod_expires.c> ExpiresActive On ExpiresByType image/gif "access plus 2 months" ExpiresByType image/jpeg "access plus 2 months" ExpiresByType image/png "access plus 2 months" ExpiresByType text/html „access plus 2 weeks“ ExpiresByType text/css "access plus 2 weeks" ExpiresByType text/javascript "access plus 2 weeks" ExpiresByType application/javascript "access plus 2 weeks" ExpiresByType application/x-javascript "access plus 2 weeks" </IfModule>JPEG, GIF and PNG image files will be cached for two months, and HTML, CSS and javascript files will be cached for two weeks. All other file types will not be cached. These directives are pretty good ones that you can directly put in your .htaccess file, if you don't feel like compiling your own directives.
When deciding which files to cache and what the caching time should be, consider how often the particular file types are changed. For example, usually media files such images, and audio and video files are not changed at all, so you can set the caching time for files from these file types (e.g. jpeg, gif, png, mp3, avi, mpeg, etc.) to several months, even up to a year. You can also set the caching time for other file types that are not changed that often to a fairly long period; for example, for files such as HTML, CSS, javascript you can set it to several weeks. It all depends on how often you modify these files. You should exclude dynamic content such as PHP scripts from being cached.
PHP and other scripts in particular may have their own instructions for controlling the HTTP headers. In this case these instructions override the ones for the particular script type in any of your .htaccess files. So for example, if you have directives for caching PHP scripts in your .htaccess file, but a particular PHP script has in its instructions directives for not caching the file, it won't be cached. In short, mod_expires is typically used for caching static content.