« Faster Form Submission and Processing with fsockopenHack WP-Cache for Maximum Speed »
Speed Tips: Add Cache-Control Headers
March 10th, 2008
Contents
Using 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
- Turn On Compression
- Add Future Expires Header
- Add Cache-Control Headers
- Turn Off ETags
- Remove Last-Modified Header
- Use Multiple SubDomains
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

With Cache-Control Headers

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>
Reader Comments
-
Thank you for the advice, hopefully it will push my page speed up :)
-
Informative article, this is the best information so far its easier to follow. Thanks
-
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!
-
What about if your using IIS based hosting!?
-
here is what I have:
- OHS webserver proxy "/" to app server, but excludes path "/static"
- on App server, there are some relatively static files served by app server
http://webserver:port/static, it is served from webserver,http://webserver:port/appstaticwill be served from app server I am trying to set cache-control headers on OHS server for bothhttp://webserver:port/staticandhttp://webserver:port/appstaticexcept FileMatch, can I use or location instead? do you have a good suggestion? -
@computerz~
problem is with Your hosting, please ask provider to check is apache module 'Header' is working correctly.
-
This works, excellent! Thank you!
-
perfect! thanks, was playing around with html meta tags, but this solution is a complete fix for all ! thank you very much!
-
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.
-
computerz ~, you need heade_module enabled on Apache!
-
probably your apache not support Header (module) computerz ~ and Peter ~
-
After many different articles and codes to add to my htaccess this one finally worked! Thank you!
Pingback:Sajal Kayan » Page Speed Analysis: Search Engine Roundtable
Pingback:Knowledge Share » Blog Archive » Improve Page Load Speed (by 80%) by Improving Component Load Speed
-
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?
-
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 ;)
-
Very nice post! This (HTTP Cache-Control w .htaccess) is just what I am looking for to add to my server, thanks for sharing!
-
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
Suggestions appreciated as I am stumped. Thank you.Internal Server Error The server encountered an internal error or misconfiguration and was unable to complete your request.
-
Thanks for these tipps. They helped me a lot! I hope my website is now faster as ever ;-)
-
Very good explanation, but I don't know why it don't works for me :( Maybe it's my hosting ...
-
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. -
Thanks that's highly useful. Cache headers can be highly useful.
-
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
-
Hello, very nice site, keep up good job! Admin good, very good.
