Apache Files Expires / Apache Set Caches
Apache is free and open-source cross-platform web server software. Read and know about Apache Files Expires module and how to set caches with Apache.
How to set caches with Apache
By properly configuring the Apache mod_expires module, you can control the cache settings of your website in visitors’ browsers. To set Cache-Control HTTP headers, you can use mod_expires directives in your server’s .htaccess file. If your website does not send these headers, browsers will request each image, CSS file, JavaScript file, or other resource from the server on every visit.
Note: Ensure the mod_expires and mod_headers modules are enabled on your server before applying these rules.
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 onThen, 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] ..."To configure caching with the Apache Expires module, use ExpiresDefault for all files, or ExpiresByType to target specific MIME types.
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. To set explicit Cache-Control directives (like public or private), use the mod_headers module.
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 cache durations:
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/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 also cache files using the FilesMatch directive. Note that the Header directive requires the mod_headers module to be enabled on your server. 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>