- Overview
- Installation & Uninstallation
- Local and remote management using IIS 7
- Context and processing order
- Apache compatibility
- Modules
- core functions
- mod_antibot
- mod_asis
- mod_auth_basic
- mod_auth_digest
- mod_authn_anon
- mod_authn_dbd
- mod_authn_default
- mod_authn_file
- mod_authz_default
- mod_authz_groupfile
- mod_authz_host
- mod_authz_user
- mod_cache
- mod_dbd
- mod_deflate
- mod_developer
- mod_dir
- mod_disk_cache
- mod_env
- mod_evasive
- mod_expires
- mod_filter
- mod_gzip
- mod_headers
- mod_hotlink
- mod_linkfreeze
- mod_log_config
- mod_logio
- mod_mem_cache
- mod_mime
- mod_proxy
- mod_replace
- mod_rewrite
- mod_seo
- mod_setenvif
- mod_so
- mod_speling
- mod_usertrack
- mod_xsendfile
- Articles
- Release history
- Troubleshooting
- License agreement
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
- Web Caching: what is it?
- How mod_cache works?
- Guide: Example of mod_cache application
- Guide: Tuning Wordpress with Helicon Ape on IIS7
- mod_disk_cache
- mod_headers
Alternate Interval Syntax
ExpiresDefault
and
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
Name | Context | Description |
---|---|---|
ExpiresActive | S V D .h | enables generation of Expires headers |
ExpiresByType | S V D .h | value of the Expires header configured by MIME type |
ExpiresDefault | S V D .h | default algorithm for calculating expiration time |
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
- MIME-type parameter may be defined as a regular expression ( notice that Apache does not support this feature ).
-
The second argument sets the number of seconds that will be added to the base time
to make up the expiration date.
Cache-Control: max-age
is calculated by subtracting the request time from the expiration date and converting the result to seconds.
Base time is either the last modification time for the file, or the time of the client's access to the document. Which of them should be used is specified in <code> field: M stands for file's last modification time and A stands for client's access time.If M value is used, all current copies of the document in all caches will expire at the same time.
If A value is used, the expiration date will be different for each client.
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