Apache Caching – Apache Caching with mod_headers and mod_expires

apache-2.2

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.

Best Answer

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>
Related Topic