Enable browser caching with .htaccess file

Using the mod_expires module, we can define how long each file type gets cached for after we’ve defined it’s MIME type.

This module controls the setting of the Expires HTTP header and the max-age directive of the Cache-Control HTTP header in server responses. The expiration date can set to be relative to either the time the source file was last modified, or to the time of the client access.

These HTTP headers are an instruction to the client about the document’s validity and persistence. If cached, the document may be fetched from the cache rather than from the source until this time has passed. After that, the cache copy is considered “expired” and invalid, and a new copy must be obtained from the source.

In the example above, you’ll see that some of the file types have “0 seconds” cache time. This is because some of the files shouldn’t be cached such as HTML and text any changes made to the copy on a website should be immediate, so we don’t cache them. Others like CSS and images will change less frequently, so we can cache them for longer periods of time.