The difference between the three .htaccess expiration rules

What is the difference between the following three .htaccess rules and when to use them, are there any special use cases that prefer one over the other ?:

Header set Cache-Control "max-age=290304000" Header set Expires "Thu, 15 Apr 2020 20:00:00 GMT" ExpiresDefault "access plus 10 years" 
+9
apache .htaccess
Sep 18 '10 at 8:41
source share
2 answers

Header is a mod_headers directive that allows you to change HTTP header fields. In this case, the Header set effectively sets the specified Cache-Control and Expire header fields, so the existing header field will be overwritten.

The first directive sets the Cache-Control header field with the value max-age=290304000 , which describes the freshness lifetime of 290304000 seconds relative to the response time.

In contrast, the second directive sets the Expires header field with a value of Thu, 15 Apr 2020 20:00:00 GMT , which describes the freshness lifetime with an absolute time value.

Both Cache-Controls maximum value and expiration date can be converted to another :

4.2.1. Life expectancy calculation

The cache can calculate the freshness lifetime (denoted as freshness_lifetime) of the response using the first match as follows:

  • ...

  • If the maximum age directive is specified ( Section 5.2.2.8 ), use its value or

  • If the Expires response header field ( Section 5.3 ) is present, use its value minus the value of the Date response header field, or

  • ...

But if both are present, the maximum age of Cache-Controls is greater compared to Expires :

If the response includes a Cache-Control field with the maximum directive age ( Section 5.2.2.8 ), the receiver MUST ignore the Expires field. Similarly, if the response includes the s-maxage directive ( Section 5.2.2.9 ), the general cache receiver MUST ignore the Expires field. In both cases, the value in Expires is intended only for recipients who have not yet implemented the Cache-Control field.

Instead of manually configuring these HTTP caching header fields, the mod_expires ExpiresDefault directive makes it easy to configure HTTP caching. The freshness lifetime can either be described with an absolute value, or with a relative value, or relative to the response time (i.e. access / now ), or relative to the modification time of the requested file (i.e. modification ). It uses both Cache-Control and Expires.

In this case, the third directive sets the default lifetime to be 10 years from the moment of response.

I would use mod_expires to control the HTTP cache instead of doing it manually with Header . This is much more convenient, allows both relative and absolute freshness times and uses both Cache-Control and Expires.

+6
Sep 19 '10 at 15:39
source share

The first rule adds a maximum cache entry to Cache-Control. The browser should restore the document after the time specified here.

The second and third rules create expiring headers. The browser should restore the document on the date indicated here. And the server has to do some calculations.

Note that the second rule forces the update for all browsers and all resources at the same time, while the third and last rule invalidates the browser cache depending on the time of the request. You will see the difference if you have a site with a lot of traffic: you are likely to get a sharp peak on April 15, 2020. :)

General: Use maximum age. It’s very well maintained and you don’t have the ability to create an invalid date. In addition, its shorter.

Further reading:

10 URLs authoritatively claimed to have expired (and probably mummified) using the expiration date "01 jan 0001".

+3
Sep 19 '10 at 13:53 on
source share



All Articles