« Faster Form Submission and Processing with fsockopenHack WP-Cache for Maximum Speed »
Speed Tips: Add Cache-Control Headers
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
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.
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>


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 fsockopen
Hack WP-Cache for Maximum Speed »
The love of liberty is the love of others; the love of power is the love of ourselves.
-- William Hazlitt
The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect. Tim Berners-Lee
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
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.
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.
Tags: Apache, askapache, Cache, Cache-Control, compression, CSS, Etags, expires header, FilesMatch, Htaccess, HTTP Headers, httpd, httpd.conf, Last-Modified, Mod_Rewrite, Optimization, PDF, Rewrite Tricks, SPEED, SSI,
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.
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 ;)