Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which one to use : Expire Header, Last Modified Header or ETags

I am running PHP on Apache, and am confused about how to implement server-side caching, in order to make the site load faster.

What is the difference between the Expires, Last-Modified and ETag headers, and which one should be used in what situation?

like image 785
Avinash Avatar asked Mar 16 '11 06:03

Avinash


People also ask

Which request header is used to check the ETag validity?

Validation with the ETag Header The HTTP ETag ("entity-tag") header is an optional HTTP header whose value is an arbitrary string that uniquely identifies one representation of the target resource.

What is the purpose of ETag header?

An ETag (entity tag) is an HTTP header that is used to validate that the client (such as a mobile device) has the most recent version of a record. When a GET request is made, the ETag is returned as a response header. The ETag also allows the client to make conditional requests.

What is last modified header?

The Last-Modified response HTTP header contains a date and time when the origin server believes the resource was last modified. It is used as a validator to determine if the resource is the same as the previously stored one.

What is an expire header?

The expires header is an HTTP header that indicates whether it is possible to obtain information on request from the browser cache or if you need to access the server since the page option in the cache is already outdated. This header contains the date and time until the page is available in the browser cache.


1 Answers

Expires and Cache-Control are "strong caching headers"

Last-Modified and ETag are "weak caching headers"

First the browser checks Expires/Cache-Control to determine whether or not to make a request to the servers.

If it has to make a request, it will send Last-Modified/ETag in the HTTP request. If the Etag value of the document matches that, the server will send a 304 code instead of 200, and no content. The browser will load the contents from its cache.

I recommend using one of the strong caching headers, along with one of the weak caching headers.

See also:

  • Google Web Fundamentals: HTTP-Caching
  • MDN web docs: HTTP caching
like image 50
hienbt88 Avatar answered Sep 16 '22 11:09

hienbt88