Speed Tips: Add Cache-Control Headers

FREE THOUGHT · FREE SOFTWARE · FREE WORLD

Speed Tips: Add Cache-Control Headers

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>

«
»

Leave your own comment

Reader Comments

  1. Samir ~

    Thanks a lot for this tips. I tried to implement this and it seems working (all previous codes generated 500 error system). I’ll see with Google Webmasters Tools if the page speed will be better in 2 weeks ;)

  2. HotCode ~

    Very nice post! This (HTTP Cache-Control w .htaccess) is just what I am looking for to add to my server, thanks for sharing!

  3. Peter ~

    Hello,
    I love this website.
    I am trying to implement caching on my site and have .htaccess uploaded to root folder with redirectmatch forwarding of *.html pages to *.php so I know my .htaccess is working.

    But when I implement:

    RedirectMatch permanent ^(.*)\.htm(l?)$ $1.php
    
    Header set Cache-Control "public"
    Header set Expires "Thu, 15 Apr 2010 20:00:00 GMT"
    

    In my .htaccess I get

    Internal Server Error The server encountered an internal error or misconfiguration and was unable to complete your request.

    Suggestions appreciated as I am stumped.

    Thank you.

  4. Aktuell ~

    Thanks for these tipps. They helped me a lot! I hope my website is now faster as ever ;-)

  5. styx ~

    Very good explanation, but I don’t know why it don’t works for me :( Maybe it’s my hosting …

  6. computerz ~

    Hi,

    I ‘ve added cache control in my .htaccess like this.


    #Header set Cache-Control "max-age=290304000, public"

    But by adding it its giving me 500 internal server error as soon as i remove it, it starts working.

    What should be the problem behind this?

    Thanks.

  7. ed neville ~

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

  8. Claude Haney ~

    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

  9. Stasigr ~

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

Go for it!


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

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. "Apache" is a trademark of The ASF. NCSA HTTPd.
UNIX ® is a registered Trademark of The Open Group. POSIX ® is a registered Trademark of The IEEE.

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

↑ TOP
Main