Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I'm working on homework for uni and was hoping someone could clarify something for me. I need to set up the following:

  • Configure the response header "Cache-Control" to have a "max-age" value of 7 days since access for all image files
  • Configure the response header "Cache-Control" to have a "max-age" value of 5 days since modification for all static HTML files.
  • Configure the response header "Cache-Control" to have a value of "public" for all static HTML and image files.
  • Configure the response header "Cache-Control" to have a value of "private" for all PHP files.

My question is whether it is better to use a FilesMatch, or the mod_expires ExpiresByType to best achieve this? I've so far used the following:

<FilesMatch "\.(gif|jpe?g|png)$">
	ExpiresDefault "access plus 7 days"
	Header set Cache-Control "public"
</FilesMatch>

<FilesMatch "\.(html)$">
	ExpiresDefault "modification plus 5 days"
	Header set Cache-Control "public"
</FilesMatch>

<FilesMatch "\.(php)$">
	Header set Cache-Control "private"
</FilesMatch>

Thanks.

share|improve this question

3 Answers 3

up vote 3 down vote accepted

You need to use the

ExpiresActive On

directive wherever you want to apply Expires headers, for example

<Location / >    
   ExpiresActive On
   ExpiresByType image/png "access plus 7 days"    
   ExpiresByType image/jpg "access plus 7 days"    
   ExpiresByType image/gif "access plus 7 days"

   ExpiresByType text/html "modification plus 5 days"

   <FilesMatch "\.(gif|jpe?g|png)$">        
      Header set Cache-Control "public"    
   </FilesMatch>

   <FilesMatch "\.(html)$">        
      Header set Cache-Control "public"    
   </FilesMatch>

   <FilesMatch "\.(php)$">
      Header set Cache-Control "private"    
   </FilesMatch> 
</Location>
share|improve this answer
    
Ah right. Thanks Dave. I was close! I had set the ExpiresActive On, so things were working. What's the difference between using ExpiresDefault like I had in the FilesMatch and ExpiresByType as a separate line like you have? I didn't do that originally as it seemed like I was repeating myself –  Aaron Moodie May 31 '09 at 4:40
    
No real different, I prefer to have all my config actions inside a <Location /> block, but it's probably more a style thing than anything else –  Dave Cheney May 31 '09 at 5:09
    
My version of Apache (2.2.15) complains: FilesMatch> cannot occur within <Location> section. Does this work for you? –  Chris Johnson Sep 17 '12 at 22:41

I know this is an old question but just in case anybody stumbles upon it...

There is a difference in using the FilesMatch and ExpiresByType directives.

FilesMatch matches a file by it's extension.

ExpiresByType matches a file by it's mime type.

It's generally better to use ExpiresByType.

Source http://www.websiteoptimization.com/secrets/advanced/caching-example.html

share|improve this answer

I'd use ExpiresByType globally instead of ExpiresDefault in a context (FilesMatch).

Have you tested your configuration?

share|improve this answer
    
hmm, no. My text/html seems to have expired 15 days ago ... so if i use ExpiresByType, say, like this: ExpiresByType text/html "access plus 5 days" can I just add in public or private at the end? ExpiresByType text/html "access plus 5 days, public" and how then do I set the header to Cache-Control? or does that happen automatically by setting ExpiresActive to on ? –  Aaron Moodie May 30 '09 at 6:57
    
OK, so my first example works, as does ExpiresByType, though with ExpiresByType i'm not sure how to add whether the file type is public or private. –  Aaron Moodie May 30 '09 at 7:22
    
You need both. (un)fortunately Dave Cheney has done your homework for you :-) –  Rich May 30 '09 at 14:37

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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