Apache Files Expires / Apache Set Caches

How to set caches with Apache

By properly configuring the Apache’s mod_expires module, you can control the cache settings of your website in visitor’s browsers. You can control this cache by configuring Cache-Control HTTP headers for your website. This can be done by adding mod_expires in your server’s .htaccess file. If your website is accessed without Cache-Control, the website will make a request to the server for each image, CSS file, Javascript file or something else to load.

Add the required rules to your .htaccess file in the root directory of the website. If there isn’t a .htaccess file, create one. The file must contain the following line:

ExpiresActive on

Then, add the necessary rules. You need the ExpiresDefault and ExpiresByType directives. Follow these syntaxes:

ExpiresDefault "base[plus num type] [num type] ..."
ExpiresByType type/encoding "base[plus num type] [num type] ..."

This is how we use Apache Expires Module. If you want to set caches for all files you can use ExpiresDefault, but if you want to set by type, use ExpiresByType.

Here, base is one of:

  • access
  • now (equivalent to 'access')
  • modification

Plus is optional. Num must be an integer value acceptable to atoi(). Type is one of the following:

  • years
  • months
  • weeks
  • days
  • hours
  • minutes
  • seconds

The ExpiresByType directive specifies the Expires header’s value and the max-age directive of the Cache-Control header generated for documents of a specified type.

The ExpiresDefault directive specifies the default algorithm to calculate the expiration time for all documents in the affected realm.

The ExpiresByType directive only has an effect when ExpiresActive On is specified. Only for the specified MIME type, it overrides any expiration date set by ExpiresDefault.

As you can see, below we set file types and set times for caches:

ExpiresActive on

# ExpiresByType text/html "access plus 600 seconds"
ExpiresByType text/css "access plus 30 days"
ExpiresByType text/javascript "access plus 30 days"
ExpiresByType application/javascript "access plus 30 days"
ExpiresByType application/x-javascript "access plus 30 days"
ExpiresByType image/gif "access plus 7 days"
ExpiresByType image/jpg "access plus 7 days"
ExpiresByType image/jpeg "access plus 7 days"
ExpiresByType image/png "access plus 7 days"
ExpiresByType application/x-shockwave-flash "access plus 7 days"

# Add a far future Expires header for fonts
ExpiresByType application/vnd.ms-fontobject "access plus 1 year"
ExpiresByType application/x-font-ttf "access plus 1 year"
ExpiresByType application/x-font-opentype "access plus 1 year"
ExpiresByType application/x-font-woff "access plus 1 year"
ExpiresByType image/svg+xml "access plus 1 year"

You can cache your files using the FilesMatch directive. Here is a simple example:

<FilesMatch "\.(gif|jpe?g|png)$">        
   Header set Cache-Control "public"    
</FilesMatch>

<FilesMatch "\.(html)$">        
   Header set Cache-Control "public"    
</FilesMatch>

<FilesMatch "\.(php)$">
   Header set Cache-Control "private"    
</FilesMatch>