mod_expires
Overview
mod_expires module is responsible for setting of the Expires HTTP
header and max-age directive of Cache-Control
HTTP header in server responses. The expiration date can be set to relative
to the time the source file was last modified or to the time of the client access.
Above HTTP headers inform the user about the document's validity and persistence.
If cached, the document may be taken from cache rather than from source until
this time has expired. After that the copy in cache is considered "expired"
and therefore invalid, and a new copy must be received from the source.
To modify Cache-Control directives other than max-age
you may use the Header directive.
Quick start
Sample .htaccess configuration to enable Expire header for html & css files
# enable expiration header
ExpiresActive On
# html expires in a 2 hour
ExpiresByType text/html A7200
# css expires in a 4 hour
ExpiresByType text/css A14400
Sample .htaccess configuration to disable Expiration for php files and enable for javascript files
<Files *.php>
ExpiresActive On
ExpiresByType text/html A0
</Files>
ExpiresActive On
# javascript expires in a 30 minutes
ExpiresByType application/x-javascript "access plus 30 minutes"
Related articles and topics
Alternate Interval Syntax
ExpiresDefaultand ExpiresByType
directives may be defined using more convenient syntax:
ExpiresDefault "<base> [plus] {<num> <type>}*"
ExpiresByType type/encoding "<base> [plus] {<num> <type>}*"
where <base> is one of the following:
- access
- now (equals 'access')
- modification
The plus keyword is optional. <num>
is an integer value and <type> is one of the
following: years, months, weeks, days, hours,
minutes, seconds.
For example, any of the following directives can be used to make documents expire 1 month after being accessed, by default:
ExpiresDefault "access plus 1 month"
ExpiresDefault "access plus 4 weeks"
ExpiresDefault "access plus 30 days"
The expiry time can be set more precisely by using several '<num>
<type>' clauses:
ExpiresByType text/html "access plus 1 month 15 days 2 hours"
ExpiresByType image/gif "modification plus 5 hours 3 minutes"
Note! If you want to set Expires header
relative to modification date, it will not be set for the content that does
not come from a file on disk because there is no modification time for such
content.
Directives
ExpiresActive
ExpiresActive directive enables or disables the generation
of Expires and Cache-Control headers for specific
location (e.g. if directive is found inside .htaccess file, it will be applied
only to documents generated from that directory).
Syntax
ExpiresActive On|Off
If set to Off, the headers will not be generated for any documents inside specific location
(unless it is overridden at lower level, e.g. .htaccess overrides server config
file). If set to On, the headers will be added to
processed documents according to the criteria defined by the
ExpiresByType
and ExpiresDefault directives.
Note! ExpiresActive
directive does not guarantee that Expires or Cache-Control
header will be generated. If the criteria aren't met, no header will be sent
and the effect will be as though the directive was not specified.
ExpiresByType
ExpiresByType directive defines the value of Expires
header and max-age directive of Cache-Control
header generated for documents of the specified type (e.g., text/html).
Syntax
ExpiresByType MIME-type <code>seconds
Description
Example
# enable expiration
ExpiresActive On
# expire GIF images after a month in client's cache
ExpiresByType image/gif A2592000
# HTML documents are good for a week from the time they were changed
ExpiresByType text/html M604800
# MIME-type with regex statement
ExpiresByType application/.* A3600
Note! This directive will only have effect if
ExpiresActive On has been specified. It overrides, for the specified MIME type only,
any expiration date set by the ExpiresDefault directive.
ExpiresDefault
ExpiresDefault directive sets the default algorithm for calculating
the expiration time for all documents in the affected location (that depends
on the location of the directive). It can be overridden on a type-by-type basis
by ExpiresByType directive.
Syntax
ExpiresDefault <code>seconds