FREE THOUGHT · FREE SOFTWARE · FREE WORLD

Home » Htaccess » Speed Tips: Add Cache-Control Headers

Speed Tips: Add Cache-Control Headers

Speed Tips: Add Cache-Control Headers Speed Tips: Add Cache-Control Headers

Speed Tips: Add Cache-Control Headers

March 10th, 2008

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>

http://www.askapache.com/htaccess/apache-speed-cache-control.html#comments

Reader Comments

  1. ECDL ~December 24, 2011 @ 4:45 pm
    Thank you for the advice, hopefully it will push my page speed up :)
  2. Philemon ~December 19, 2011 @ 3:30 am
    Informative article, this is the best information so far its easier to follow. Thanks
  3. Andärin ~December 7, 2011 @ 11:45 pm
    Thanks for this. I had troubling finding some good example code for setting the Cache-Control, but this did the trick. Now my blog and gallery are totally cached! Thanks again!
  4. Computer Rerpair Dublin ~November 9, 2011 @ 11:38 pm
    What about if your using IIS based hosting!?
  5. testohs ~August 12, 2011 @ 7:37 pm
    here is what I have:
    1. OHS webserver proxy "/" to app server, but excludes path "/static"
    2. on App server, there are some relatively static files served by app server
    so If url: http://webserver:port/static, it is served from webserver, http://webserver:port/appstatic will be served from app server I am trying to set cache-control headers on OHS server for both http://webserver:port/static and http://webserver:port/appstatic except FileMatch, can I use or location instead? do you have a good suggestion?
  6. macem ~May 13, 2011 @ 1:03 pm

    @computerz~

    problem is with Your hosting, please ask provider to check is apache module 'Header' is working correctly.

  7. Richard ~May 5, 2011 @ 7:36 am
    This works, excellent! Thank you!
  8. hoogste spaarrente ~April 17, 2011 @ 3:15 am
    perfect! thanks, was playing around with html meta tags, but this solution is a complete fix for all ! thank you very much!
  9. Jodoh ~December 5, 2010 @ 8:17 pm
    Thank you very much, Askapache. Those line very help me and my improved site speed. Your tutorial always nice and smart (I read your site since 2 years ago and use it's tip every where) Thank you.
  10. Lucas Arruda ~September 14, 2010 @ 1:27 pm
    computerz ~, you need heade_module enabled on Apache!
  11. macem ~September 14, 2010 @ 8:09 am
    probably your apache not support Header (module) computerz ~ and Peter ~
  12. Christine ~September 2, 2010 @ 3:25 am
    After many different articles and codes to add to my htaccess this one finally worked! Thank you!
  13. Pingback:Sajal Kayan » Page Speed Analysis: Search Engine Roundtable

  14. Pingback:Knowledge Share » Blog Archive » Improve Page Load Speed (by 80%) by Improving Component Load Speed

  15. Thompson ~July 16, 2010 @ 6:04 pm
    Will this interfere with W3 Total cache or other caching plugins? For instance, if I tell the plugin to clear the page cache, will old content still be cached locally by users? What's the best way to make them play nice together?
  16. Samir ~May 11, 2010 @ 1:45 pm
    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 ;)
  17. HotCode ~May 7, 2010 @ 11:57 am
    Very nice post! This (HTTP Cache-Control w .htaccess) is just what I am looking for to add to my server, thanks for sharing!
  18. Peter ~April 16, 2010 @ 2:51 am
    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
    &nbsp;
    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.
  19. Aktuell ~December 4, 2009 @ 4:39 am
    Thanks for these tipps. They helped me a lot! I hope my website is now faster as ever ;-)
  20. styx ~December 2, 2009 @ 5:35 pm
    Very good explanation, but I don't know why it don't works for me :( Maybe it's my hosting ...
  21. computerz ~July 3, 2009 @ 9:14 am
    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.
  22. ed neville ~January 13, 2009 @ 8:55 am
    Thanks that's highly useful. Cache headers can be highly useful.
  23. Claude Haney ~November 5, 2008 @ 2:16 am
    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
  24. Stasigr ~October 29, 2007 @ 8:13 am
    Hello, very nice site, keep up good job! Admin good, very good.

Add Comment!

Leave a Reply

Your email address will not be published.


Google +

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

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 |

↑ TOPMain