Summary
This module controls the setting of the Expires HTTP header in server responses. The expiration date can be set relative to either the time that the source file was last modified, or relative to the time that the client accessed the server.
The Expires HTTP header is an instruction to the client regarding the document's validity and persistence. If cached, the document may be retrieved from the cache rather than from the source until the allocated time has passed. After this occurs, the cache copy is considered "expired" and a new copy must be obtained from the source.
Alternate Interval Syntax
The ExpiresDefault and ExpiresByType directives can also be defined in a more readable syntax of the form:
ExpiresDefault "<base> [plus] {<num> <type>}*" ExpiresByType type|encoding "<base> [plus] {<num> <type>}*"
The <base> argument is one of the following:
The [plus] keyword is optional. The <num> argument should be an integer value [acceptable to atoi()], and <type> is one of the following:
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 fine-tuned by adding several <num> and <type> arguments. For example:
ExpiresByType text/html"access plus 1 month 15 days 2 hours" ExpiresByType image/gif "modification plus 5 hours 3 minutes"
Directives
Module: mod_expires | |
Syntax: ExpiresActive on | off | |
Default: ExpiresActive off | |
Context: server config, virtual host, directory, .htaccess | |
Override: Indexes | |
Origin: Apache | |
Example: ExpiresActive on |
The ExpiresActive directive enables or disables the generation of the Expires header for the document realm in question. If this directive is found in an .htaccess file it only applies to documents generated from that directory.
Module: mod_expires | |
Syntax: ExpiresByType MIME-type code seconds | "<base> [plus] <num> <type>" | |
Default: none | |
Context: server config, virtual host, directory, .htaccess | |
Override: Indexes | |
Origin: Apache | |
Example: ExpiresByType image/gif A2592000 | |
Example: ExpiresByType text/html "access plus 30 days" |
The ExpiresByType directive defines the value of the Expires header generated for documents of the specified type ( for example, text/html). The second argument sets the number of seconds that will be added to a base time to construct the expiration date.
The base time is either the last modification time of the file, or the time of the client's access to the document. Whether access time or modification time should be used is specified by the code field. M means that the file's last modification time should be used as the base time, and A means the client's access time should be used.
The difference in effect between the A and the M is minimal. If M is used, all current copies of the document in all caches will expire at the same time. This could be useful for something like a weekly notice that is always found at the same URL. If A is used, the date of expiration is different for each client. This could be useful for image files that do not change very often, particularly for a set of related documents that all refer to the same images ( for example, the images will be accessed repeatedly within a relatively short time span).
Here is an example to specify the expiration time calculation. For examples using alternate syntax, see the beginning of this article.
#enable expirations ExpiresActive on #expire GIF images after a month in the clients cache ExpiresByType image/fig A2592000 #HTML documents are good for a week from the time they were changed ExpiresByType text/html M604800
Module: mod_expires | |
Syntax: ExpiresDefault code seconds| "<base> [plus] <num> <type>" | |
Default: none | |
Context: server config, virtual host, directory, .htaccess | |
Override: Indexes | |
Origin: Apache | |
Example: ExpiresDefault A2592000 | |
Example: ExpiresDefault "access plus 1 month" |
The ExpiresDefault directive sets the default algorithm for calculating the expiration time for all documents in the affected realm. It can be overridden on a type-by-type basis by the ExpiresByType directive. See the description of the ExpiresByType directive for details about the syntax of the argument, and the alternate syntax description as well.