Why mod_expires is important
Firstly in order to truly understand mod_expires you must know what browser caching is. When you access any website your browser has a cache memory in which it stores the static contents of this website in order to speed up the loading process the next time you access that website.
As many static elements such as images, css and js files are used site-wide you will experience faster load time while browsing the website as you do not need to download these files for each page you visit. The cache is controlled by the Cache-Control HTTP headers of the website which is done by mod_expires when running your site on an Apache-based hosting server.
Now that we have cleared that out of the way, we can proceed with the clarification of mod_expires. By adding mod_expires module to your .htacsess file you can control the expiration time of certain elements in your client’s browser cache via cache-control HTTP headers.
How to setup expire dates for browser’s cache
As said that is done by configuring the HTTP Cache-Control headers. Here is a simple implementation in the .htaccess file:
<ifmodule mod_expires.c> ExpiresActive On // shows the activation/deactivation of cache-control & expires headers ExpiresDefault "access plus 2 days" </ifmodule>
Explanation of the code:
ExpiresActive On – an ‘expires’ header that shows the activation/deactivation of cache-control & other expires headers
ExpiresDefault ”access plus 2 days” – sets the default lifespan for all cached data to 2 days. You can change the time period to e.g. 1 month, 1 year, etc just by switching the “2 days” part of the code with your desired period. You can also define more precise expiration time by simply concatenating it to the code(e.g ExpiresDefault ”access plus 2 days 5 hours 3 minutes” ). The smallest time unit that you can specify is ‘seconds’, while the largest is ‘years’.
Another important thing is that ‘access’ can be changed for ‘modification’. If you use a ‘modification’ date based setting, the Expires header will not be added to content that does not come from a file on disk. This is due to the fact that there is no modification time for such content.
While this is a simple solution it is not refined. Since there is content that don’t want to be updated so often like images and videos. The solution is to use a header that specifies for what type of content for which you apply the expiration time:
ExpiresByType type/encoding "base [plus num type] [num type] …”
Where ‘type/coding’ is the type of the data, which you want to be affected by the expiration timer(e.g. text/html, image/gif, application/<some application>, etc.)
To make the mod_expires easier for you, here is an example which you can directly copy and paste into your .htaccess file:
The following expires headers are set pretty far in the future. If you don't control versioning with filename-based cache busting, consider lowering the cache time for resources like CSS and JS to something like 1 week.
mod_expires is fully supported on all FastComet Shared Hosting plans. In case you experience any difficulties to enable mod_expires for your account or website, please feel free to contact us via your client area by submitting a ticket to our technical support team.