.htaccess is a configuration file for use on web servers running the Apache Web Server software. .htaccess files can be used to alter the configuration of the Apache Web Server software to enable/disable additional functionality and features that the Apache Web Server software has to offer.
Knowing how to configure the .htaccess file allows for better control of assets that are cached. This article will serve as a guide to configure the default headers and well as custom expiry headers to control your caching behavior.
Custom Expiry Headers
When using Origin Pull, there is a default TTL (Expiry Date for the cached files) of 24 hours. There is also the option to override that or set it if not already set on the server. But how do you set a custom expiry header on your server, for a certain file type, using Apache .htaccess (or httpd.conf for advanced users with administrative access to their hosting servers)?
TTL/Expiry Header
This is fairly easy. One can set an expiry header to a certain file, or a certain file type, or multiple file types to any length desired, and the CDN will honor this header.
If you wish to make the TTL of 12 hours, for example, that would be 60*60*12 = 43200 seconds.
-
Example:
ExpiresActive On ExpiresDefault A43200 Header set Cache-Control "no-store, no-cache, must-revalidate, max-age=0" Header set Pragma "no-cache"
In the above example, “testcdn.txt” is our sample file, and “A43200” is our expiry header. - If we want to do that for certain JavaScript files, or CSS files, we use the “FilesMatch” tag and we use a regular expression for the file type. Example:
ExpiresActive On <filesmatch "\.(js|css)$"=""> ExpiresDefault A43200 </filesmatch>
- If we want a file to NOT be cached, we will simply set an expiry header to 0 seconds. Example:
OrExpiresActive On ExpiresDefault A0 Header set Cache-Control "no-store, no-cache, must-revalidate, max-age=0" Header set Pragma "no-cache"
<filesmatch "\.(js)$"=""> ExpiresDefault A0 Header set Cache-Control "no-store, no-cache, must-revalidate, max-age=0" Header set Pragma "no-cache" </filesmatch>
If you have any questions or experience any issues, please reach out to the Support Team, live chat and ticket support are available 24/7.