3

We are trying to set Cache-Control header: max-age=300, public to all our public site pages. To use Filesmatch, my applciation pages do not have any extensions. ExpiresByType is available, but it has its own disadvantages.

I am looking for a way to set cache control header to all my application pages with content type as text/html. Is there any way to achieve this?

skonka
  • 91
  • 1
  • 4

3 Answers3

4

A safer way (because developers can make misstakes when setting Content-Type for file extensions) is to set header based on the actual Content-Type:

<IfModule mod_headers.c>
  Header set Cache-Control "max-age=300, public" "expr=%{CONTENT_TYPE} =~ m#text/html#i"
</IfModule>
Null
  • 141
  • 3
1

The browser doesn't need to see a .html extension for it to know it is a text/html mime type document. As long as the header broadcasts to the client browser that the document is indeed of mime type text/html, this will do just fine:

ExpiresByType text/html "access plus 300 seconds"

If you elaborate on the "has its own disadvantages" part, we can perhaps comment on that too.

JayMcTee
  • 3,763
  • 12
  • 20
  • I do not have mod_expires.so module in our apache config. It's been refined and only option I have is to control by setting max-age in cache-control header. Is there any way that I can do this? – skonka Jun 18 '15 at 11:24
  • You don't mention the technology behind your application, but for example in PHP you can set the cache control headers directly in the code: http://stackoverflow.com/questions/4480304/how-to-set-http-headers-for-cache-control eg. header("Cache-Control: max-age=300"); – JayMcTee Jun 18 '15 at 12:01
  • Our pages are all HTML, rendered from a content management system. We have apache to maintain our static content, rewrite rules, reverse proxy etc., – skonka Jun 18 '15 at 14:30
0

As you can't use mod_expires, maybe you can use mod_headers instead : http://httpd.apache.org/docs/2.2/mod/mod_headers.html.

You can use filesMatch combined with header

<filesMatch "\\.(html|htm)$">
Header set Cache-Control "max-age=300, public"
</filesMatch>
Froggiz
  • 3,013
  • 1
  • 18
  • 30
  • I have been using FilesMatch, but an interesting thing to note is that unless your html pages have ".html" (or ".htm") extension, this branch won't be invoked. It's better to use a Content-Type of text/html to do the matching – Dagmar May 18 '20 at 11:45
  • Yes, this way you apply the rule only on .htm and .html files, this can prevent dynamic pages (like php) to be cached if they send html headers – Froggiz May 19 '20 at 12:12