1

Some questions on the absolute, optimal usage of caching of dynamic and static cache.

Currently, my htaccess is setup as below, and I have the feeling im not really using it the correct way... Any of your improvement-suggestions I see as answers! Much appreciated!

Question1: any suggestions as to what I should change, in order to make caching more correct within the two levels ( #### dynamic / #### static)

Question 2: what does these IF tags do: <IfModule mod_headers.c> ... </IfModule>
Is it advisable to keep or not? Without it seems to also work. Are there speed advantages using this?

Question 3: Should I use both ExpiresDefault M7200 and Header set Cache-Control "max-age=7200" or will either one do?


<IfModule mod_headers.c>

    ExpiresActive On
    Header unset ETag
    FileETag None
    Header unset Pragma
    ExpiresDefault M14515200

    ##### DYNAMIC PAGES
    <FilesMatch "\\.(htm|php)$">
        ExpiresDefault M7200
        Header set Cache-Control "max-age=7200"
    </FilesMatch>

    ##### STATIC FILES
    <FilesMatch "\\.(ico|jpg|png|gif|svg|swf|css|js|fon|ttf|eot|xml|pdf|flv)$">
        ExpiresDefault M14515200
        Header set Cache-Control "max-age=14515200"
    </FilesMatch>

</IfModule>
splattne
  • 28,508
  • 20
  • 98
  • 148
Sam
  • 423
  • 3
  • 7
  • 23

1 Answers1

2

To answer question 2 first, the <IfModule> wrapper is there so that your config will still work (albeit without the bits that set headers) on an Apache instance that doesn't include mod_headers. Without the wrapper, an Apache instance without mod_headers would fail on startup.

For question 3, web servers set both Expires and Cache-Control headers because the history of caching headers is long and muddled, and covering both of them is your best bet for getting as many end users as possible to respect your cache lifetimes. 99% of the time, either one will be enough (in which case you might as well use max-age, and push the CPU workload of determining what is 7200 minutes from now off to the user's browser rather than your server).

For question 1, if your .htm and .php pages are really dynamic (contents depend on who the user is or what they're doing), then you shouldn't be allowing them to be cached at all. .xml files are often generated by your code, and if so then they should probably be included in the dynamic rather than static content. And it's only OK to give your "static" files a such a long lifetime if you've taken steps to ensure that they really are static, and you can never change the contents of a file while keeping the same filename. In particular, if you change your JavaScript or CSS files, then users will see unexpected results depending on what they've got cached and what they don't.

Mike Scott
  • 7,993
  • 31
  • 26
  • Just clarify that when you say "..that they really are static, and you can never change the contents of a file while keeping the same filename" it should really say file modification date. The browser would compare the one reading with the one on cache (or the e-tag header alternative, which is an MD5 of the files timestamp) to check if the file on cache is the same + if the time it cached it + max-age should make it avoid downloading it again. – luison Feb 22 '12 at 15:07