Setting Custom Expiry Headers using .htaccess

When using Origin Pull, you get a default TTL (Expiry Date for the cached files) of 24 hours. You also get 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)?

Setting a default TTL/Expiry Header in .htaccess:

This is fairly easy. You 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.

  1. 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.

  2. 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>
  3. If we want a file to NOT be cached, we will simply set an expiry header to 0 seconds.
    Example:

    
            ExpiresActive On
            ExpiresDefault A0
            Header set Cache-Control "no-store, no-cache, must-revalidate, max-age=0"
            Header set Pragma "no-cache"

    Or

    
            <filesmatch "\.(js)$"="">
                ExpiresDefault A0
                Header set Cache-Control "no-store, no-cache, must-revalidate, max-age=0"
                Header set Pragma "no-cache"
            </filesmatch>