10

I have used this before;

<IfModule mod_expires.c>
  ExpiresActive On
  ExpiresByType image/gif "now plus 2 weeks"
  // Lots omitted here
</IfModule>

And this;

<IfModule mod_headers.c>
  <filesMatch "\\.(ico|pdf|flv|jpg|jpeg|png|gif|swf|JPG)$">
    Header set Cache-Control "max-age=1209600"
  </filesMatch>
  // Lots omitted here
</IfModule>

I can set the expires on by content-type and I can set any header I wish by file extension.

But neither of these seem to let you set any header you want by content-type.

I want to set the cache-control header based on the content-type of the response - note that this is not the same as the file extension. I have "friendly URLs" so there is no file extension to be captured by filesMatch so there is no file extension but the content-type is text/html.

How can I set the cache-control header for specific content-types?

2
  • Does the Cache-Control header exist in your response?
    – chris.tian
    Feb 25, 2014 at 14:29
  • "How can I set the cache-control header for specific content-types?" - This is what the ExpiresByType directive does (well, specifically, it sets the max-age directive of the Cache-Control header, as well as the Expires header for backward compatibility). However, if you wanted to set "any header" or specific directives in the Cache-Control header then you would need a different method.
    – MrWhite
    Feb 23, 2018 at 22:35

3 Answers 3

9
+100

In 2.4, you can append expr= to the Header directive instead of env=. For example:

Header set Cache-Control "max-age=3600" "expr=%{CONTENT_TYPE} == 'text/html'"

In the default (non-early) mode, mod_headers runs as an output filter – so the content type is already set and available by the expression parser.

http://httpd.apache.org/docs/2.4/expr.html

3
  • Thanks, but I am on version 2.2
    – Jake N
    Feb 10, 2014 at 16:09
  • The Apache 2.4 expression docs say that CONTENT_TYPE is not available during <If>. I have found this to be inaccurate, it is available during <If> (2.4.52).
    – Jeff
    Feb 25, 2022 at 22:19
  • @Jeff I don't think this can be true aside from something like a contrived case where you have "header set early Content-Type". pastebin.com/3kP2syS3 demonstrates the current behavior that matches the manual On a conceptual level: <If> expressions are evaluated before all of the following happen, so there should be no meaningful content-type available to query 1. before request is mapped to disk 2. before the"type_checker" hook is called to allow modules to guess content types 3. before the "handler" (CGI, proxy, etc) has run which will set the content-type during processing
    – covener
    Feb 27, 2022 at 3:40
-1

I guess you will need to append or set the Cache-Control header first. Please try the snippet below and do not forget the "no-transform" param.

<IfModule mod_expires.c>
   ExpiresActive On
   ExpiresByType image/gif "now plus 2 weeks"
  // Lots omitted here

  //This is the magic
  <IfModule mod_headers.c>
     Header append Cache-Control "public, no-transform"
  </IfModule>

</IfModule>
2
  • 2
    I don't this helps at all either. Header append Cache-Control "public, no-transform" does not take into account the content-type?
    – Jake N
    Feb 25, 2014 at 23:23
  • Note that before Apache 2.2.12 it was not possible to set the Cache-Control header with the Header directive.
    – MrWhite
    Feb 23, 2018 at 22:34
-2

If you want to make the cache content-type you can enter it in this way:

<IfModule mod_expires.c>

ExpiresActive on

ExpiresByType text/html "access plus 15 days"

</IfModule>
2
  • This sets the ExpiresByType header. This is not what I need. I need to be able to set the Cache-Control By the content-type.
    – Jake N
    Feb 25, 2014 at 23:21
  • 1
    @JakeN "This sets the ExpiresByType header." - I guess you meant Expires header, ExpiresByType is the mod_expires directive. But even on Apache 2.0, this should also set the Cache-Control header (by the mime-type / Content-Type). AFAIK you'd have to go back to Apache 1.3 for this to only set the Expires header.
    – MrWhite
    Feb 23, 2018 at 22:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.