Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set a header by Content-Type

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?

like image 930
Jake N Avatar asked Feb 09 '14 18:02

Jake N


1 Answers

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

like image 64
covener Avatar answered Oct 09 '22 18:10

covener