Speed Tips: Add Cache-Control Headers

FREE THOUGHT · FREE SOFTWARE · FREE WORLD

Cache-Control Headers using Apache and .htaccess – Easy Apache Speed TipsMarch 10th, 2008

« Faster Form Submission and Processing with fsockopenHack WP-Cache for Maximum Speed »

Cache-Control Headers OnUsing Cache-Control headers you can specify which types of proxies can cache certain content, and how long files should be cached. You can also add the must-revalidate header to force checking the ETag and or Last-Modified for the file to make sure the cache is using the current version.

Easy Apache Speed Tips Articles

Keep in mind, if you use a far future Expires header you have to change the component’s filename whenever the file changes. So please add must-revalidate to your Cache-Control header for your .html files.

Add Cache-Control Headers

This goes in your root .htaccess file but if you have access to httpd.conf that is better.

This code uses the FilesMatch directive and the Header directive to add Cache-Control Headers to certain files.

# 480 weeks
<FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf)$">
Header set Cache-Control "max-age=290304000, public"
</FilesMatch>
 
# 2 DAYS
<FilesMatch "\.(xml|txt)$">
Header set Cache-Control "max-age=172800, public, must-revalidate"
</FilesMatch>
 
# 2 HOURS
<FilesMatch "\.(html|htm)$">
Header set Cache-Control "max-age=7200, must-revalidate"
</FilesMatch>

If you are using far Future Expires Headers and Cache-Control (recommended), you can do this for these files.

<FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf)$">
Header set Cache-Control "public"
Header set Expires "Thu, 15 Apr 2010 20:00:00 GMT"
</FilesMatch>

Without Cache-Control Headers

No Cache-Control Headers

With Cache-Control Headers

Cache-Control Headers On

Currently AskApache.com Uses

You can use the HTTP Header Viewer Tool to check it.

Header unset Pragma
FileETag None
Header unset ETag
 
# 1 YEAR
<FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|swf|mp3|mp4)$">
Header set Cache-Control "public"
Header set Expires "Thu, 15 Apr 2010 20:00:00 GMT"
Header unset Last-Modified
</FilesMatch>
 
# 2 HOURS
<FilesMatch "\.(html|htm|xml|txt|xsl)$">
Header set Cache-Control "max-age=7200, must-revalidate"
</FilesMatch>
 
# CACHED FOREVER
# MOD_REWRITE TO RENAME EVERY CHANGE
<FilesMatch "\.(js|css)$">
Header set Cache-Control "public"
Header set Expires "Thu, 15 Apr 2010 20:00:00 GMT"
Header unset Last-Modified
</FilesMatch>

« Faster Form Submission and Processing with fsockopenHack WP-Cache for Maximum Speed »

Reader Comments

  1. ed neville says:

    Thanks that’s highly useful. Cache headers can be highly useful.

  2. Claude Haney says:

    I had AOL Hosting FTP – Now new to DSN Hosting – got a good HOST – but when I upload a “OVERWRITE” file replacement – it taks at least 4- 6 hours for the replacement file to take hold and show. They say this is cause AOL use ‘Proxy Caching Servers’ which hold the last known version of a web page. I don’t understand the code I need to overcome this caching – I have tried cleaning all caches browser and AOL – but still don’t work.

    I’m sure the answer is in the above box – Easy Apache Speed Tips Articles – but don’t understand – I know HTML code pretty well and never use an Editor. I’m lost here. Any help would really be appreciated..

    Claude

  3. [...] Add Cache-Control Headers [...]

  4. [...] Add Cache-Control Headers [...]

  5. Stasigr says:

    Hello, very nice site, keep up good job!
    Admin good, very good.

RSS feed for comments on this post.TrackBack URL

Add Your Opinion

Skip to Comments
1399

It's very simple -
you read the protocol
and write the code.
-Bill Joy

HTML | DCMI | GRDDL | XOXO | XDMP | XFN | DOM | XML | XHTML 1.1 Strict | CSS 2.1 | W3C | WAI | DISA | ICSI | GIAC | SANS RR | GHOST

Authority: 110

↑ TOP

Except where otherwise noted, content on this site is licensed under a Creative Commons Attribution 3.0 License, just credit with a link.
This site is not supported or endorsed by The Apache Software Foundation (ASF). All software and documentation produced by The ASF is licensed according to these terms. "Apache" is a trademark of The ASF.

Site Map | Contact Webmaster | Glossary | License and Disclaimer | Terms of Service