Apache Module mod_expires

Updated on Mar 13, 2019

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.

If no Cache-Control is set for the HTTP headers your visitors will have longer wait time when visiting your website as they will need to download static content such as images, CSS and javascript files for each page they access.

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 and 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 "2days" 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:

<IfModule mod_expires.c>

    ExpiresActive on
    ExpiresDefault                                      "access plus 1 month"

  # CSS
    ExpiresByType text/css                              "access plus 1 year"

  # Data interchange
    ExpiresByType application/json                      "access plus 0 seconds"
    ExpiresByType application/xml                       "access plus 0 seconds"
    ExpiresByType text/xml                              "access plus 0 seconds"

  # Favicon (cannot be renamed!)
    ExpiresByType image/x-icon                          "access plus 1 week"

  # HTML components (HTCs)
    ExpiresByType text/x-component                      "access plus 1 month"

  # HTML
    ExpiresByType text/html                             "access plus 0 seconds"

  # JavaScript
    ExpiresByType application/javascript                "access plus 1 year"

  # Manifest files
    ExpiresByType application/x-web-app-manifest+json   "access plus 0 seconds"
    ExpiresByType text/cache-manifest                   "access plus 0 seconds"

  # Media
    ExpiresByType audio/ogg                             "access plus 1 month"
    ExpiresByType image/gif                             "access plus 1 month"
    ExpiresByType image/jpeg                            "access plus 1 month"
    ExpiresByType image/png                             "access plus 1 month"
    ExpiresByType video/mp4                             "access plus 1 month"
    ExpiresByType video/ogg                             "access plus 1 month"
    ExpiresByType video/webm                            "access plus 1 month"

  # Web feeds
    ExpiresByType application/atom+xml                  "access plus 1 hour"
    ExpiresByType application/rss+xml                   "access plus 1 hour"

  # Web fonts
    ExpiresByType application/font-woff                 "access plus 1 month"
    ExpiresByType application/vnd.ms-fontobject         "access plus 1 month"
    ExpiresByType application/x-font-ttf                "access plus 1 month"
    ExpiresByType font/opentype                         "access plus 1 month"
    ExpiresByType image/svg+xml                         "access plus 1 month"

</IfModule>

Note:

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.

On this page...

    Optimized SSD Web Hosting

    • Free Domain Transfer
    • 24/7 Technical Support
    • Fast SSD Storage
    • Hack-free Protection
    • Free Script Installation
    • Free Website Transfer
    • Free Cloudflare CDN
    • Immediate Activation
    View More