FREE THOUGHT · FREE SOFTWARE · FREE WORLD

Home  »  Hacking  »  Speed up your site with Caching and cache-control

by 35 comments

Caching with .htaccess and Apache will take your website and your web skills to the next level. This is some technical and advanced methods condensed to simple htaccess code examples for you. But you must take the time to understand caching with cache-control and other headers and HTTP options before you implement on a production server.

More detailed article: Speed Up Sites with htaccess Caching.

Easy Apache Speed Tips Articles


Caching with both mod_expires + mod_headers

# Turn on Expires and set default to 0
ExpiresActive On
ExpiresDefault A0
 
# Set up caching on media files for 1 year (forever?)
<FilesMatch "\.(flv|ico|pdf|avi|mov|ppt|doc|mp3|wmv|wav)$">
ExpiresDefault A29030400
Header append Cache-Control "public"
</FilesMatch>
 
# Set up caching on media files for 1 week
<FilesMatch "\.(gif|jpg|jpeg|png|swf)$">
ExpiresDefault A604800
Header append Cache-Control "public"
</FilesMatch>
 
# Set up 2 Hour caching on commonly updated files
<FilesMatch "\.(xml|txt|html|js|css)$">
ExpiresDefault A7200
Header append Cache-Control "proxy-revalidate"
</FilesMatch>
 
# Force no caching for dynamic files
<FilesMatch "\.(php|cgi|pl|htm)$">
ExpiresActive Off
Header set Cache-Control "private, no-cache, no-store, proxy-revalidate, no-transform"
Header set Pragma "no-cache"
</FilesMatch>

Caching with mod_headers

# 1 YEAR
<FilesMatch "\.(flv|ico|pdf|avi|mov|ppt|doc|mp3|wmv|wav)$">
Header set Cache-Control "max-age=29030400, public"
</FilesMatch>
 
# 1 WEEK
<FilesMatch "\.(jpg|jpeg|png|gif|swf)$">
Header set Cache-Control "max-age=604800, public"
</FilesMatch>
 
# 3 HOUR
<FilesMatch "\.(txt|xml|js|css)$">
Header set Cache-Control "max-age=10800"
</FilesMatch>
 
# NEVER CACHE
<FilesMatch "\.(html|htm|php|cgi|pl)$">
Header set Cache-Control "max-age=0, private, no-store, no-cache, must-revalidate"
</FilesMatch>

Caching with mod_expires

ExpiresActive On
ExpiresDefault A0
 
# 1 YEAR
<FilesMatch "\.(flv|ico|pdf|avi|mov|ppt|doc|mp3|wmv|wav)$">
ExpiresDefault A29030400
</FilesMatch>
 
# 1 WEEK
<FilesMatch "\.(jpg|jpeg|png|gif|swf)$">
ExpiresDefault A604800
</FilesMatch>
 
# 3 HOUR
<FilesMatch "\.(txt|xml|js|css)$">
ExpiresDefault A10800"
</FilesMatch>

Other Speed Tips

To truly speed up your site, you will want to implement a server-side caching technique. Or you can read more about caching and web cache.

Apache Caching Guide

This document supplements the <a href="http://askapache.info/trunk/mod/mod_cache.html" title="Apache mod_cache">mod_cache</a>, <a href="http://askapache.info/trunk/mod/mod_disk_cache.html" title="Apache mod_disk_cache">mod_disk_cache</a>, <a href="http://askapache.info/trunk/mod/mod_mem_cache.html" title="Apache mod_mem_cache">mod_mem_cache</a>, <a href="http://askapache.info/trunk/mod/mod_file_cache.html" title="Apache mod_file_cache">mod_file_cache</a> and htcacheclean reference documentation.

It describes how to use Apache's caching features to accelerate web and proxy serving, while avoiding common problems and misconfigurations.

htaccess time cheatsheet

#      300   5 MIN
#      600  10 MIN
#      900  15 MIN
#     1800  30 MIN
#     2700  45 MIN
#     3600   1 HR
#     7200   2 HR
#    10800   3 HR
#    14400   4 HR
#    18000   5 HR
#    36000  10 HR
#    39600  11 HR
#    43200  12 HR
#    46800  13 HR
#    50400  14 HR
#    54000  15 HR
#    86400   1 DAY
#   172800   2 DAY
#   259200   3 DAY
#   345600   4 DAY
#   432000   5 DAY
#   518400   6 DAY
#   604800   1 WEEK
#  1209600   2 WEEK
#  1814400   3 WEEK
#  2419200   4 WEEK
#  4838400   2 MONTH
#  7257600   3 MONTH
#  9676800   4 MONTH
# 12096000   5 MONTH
# 14515200   6 MONTH
# 16934400   7 MONTH
# 19353600   8 MONTH
# 21772800   9 MONTH
# 24192000  10 MONTH
# 26611200  11 MONTH
# 29030400  12 MONTH

Tags

April 10th, 2007

Comments Welcome

  • Daniel

    I couldn't understand some parts of this article o.us poetry, but I guess I just need to check some more resources regarding this, because it sounds interesting.

  • Jason

    Thanks for the article. I have been searching for a long time trying to figure out how to use header expires, this was extremely helpful. You can count on me for a few links here and there!

    • http://www.netlash.com Erik Bauffman

      I was wondering. Say I want to my .css files to be cached for 15 minutes, my .html files for another 30 minutes and no cache at all for my .php files. How can I do this easily ?

  • http://www.webtlk.com Frank

    I find these code quite interesting and i have noticed a better performance in my blog already. What i would like to know if i can use the first code at the beginning of this page:

    # Turn on Expires and set default to 0
    ExpiresActive On
    ExpiresDefault A0
     
    # Set up caching on media files for 1 year (forever?)
    ExpiresDefault A29030400
    Header append Cache-Control "public"
     
    # Set up caching on media files for 1 week
    ExpiresDefault A604800

    ... with this other one:

    # 1 YEAR
    Header set Cache-Control "max-age=29030400, public"
     
    # 1 WEEK
    Header set Cache-Control "max-age=604800, public"
     
    # 2 DAYS
    Header set Cache-Control "max-age=172800, proxy-revalidate"
     
    # 1 MIN
    Header set Cache-Control "max-age=60, private, proxy-revalidate"

    Since i am not into these things it looks like if i use both of them my website is faster, but i would like an input from you to see if i am right or now.

    Also, i understand the second code i wrote in this comment but.. what is the difference between these two codes simply put? thanks and i hope to get a reply from you

  • fara

    thanks... very useful article that saved me from searching.
    good luck

  • http://www.e-tigara.com Tig

    One of the greatest articles i found on the web. Thanks.

  • http://www.ericgillette.com Eric Gillette

    This definitely helped!

    I have a client with a wildy popular blog and they were having lots of site slow-downs.

    After setting this up in httpd.conf, the rest was history!

    Everything is a bit faster now! Thanks so much.

  • http://www.pricespakistan.com/ Aneeza Mubbashir

    Thanks for the article. I have been searching for a long time trying to figure out how to use header expires, this was extremely helpful. You can count on me for a few links here and there!

  • http://www.reviewmonsters.co.uk Review Monsters

    Thank you for this post, easy to follow and has really decreased our load time. Thank you again!

  • http://www.thefunbooster.com ravi

    this solved my problem .thanks for this useful post

  • Danny

    I used Caching with both mod_expires + mod_headers but I received a 500 error. Website is being hosted by earthlink.

  • http://www.flasheur.ch Flasheur

    Hi, very interesting stuff! I just noticed a little mistake in the "caching with mod_expires" part, the ExpiresDefault parameter for 1 year should be A29030400 instead of A9030400 according to the time cheatsheet. Thanks again for your article.

  • tophi

    And what about ppl using Nginx ?

  • http://www.citizenthrift.com Kristy

    I found this site as I was editing the .htaccess to improve performance and to block spammer and proxies.

    Your site explained it very well. Almost everywhere else was just badly written.

    Perhaps if your interested I can send you an optimized htaccess file that blocks most content scraper, spam bots and lots of caching (browser as well as server side)...

  • Mike

    What if I want to disable cache specifically for a single IP address, while everyone else who browses the site gets a standard length? Right now I'm trying this code below, but it's disabling the cache for everyone, not just the intended target.

    allow from xxx.xxx.xxx.xxx
    Header set Cache-Control "private, no-cache, no-store, proxy-revalidate, no-transform"
    Header set Pragma "no-cache"
    allow from all
    Header set Cache-Control "max-age=86400, public"

    Is this possible?

    • http://www.askapache.com/ AskApache

      @Mike

      Yes. Try using the RequestHeader to rewrite the cache headers for specific IPs. Or set an environment variable when the visitor IP is the single IP to target, then only send the various anti-caching Header directives when that environment variable is present.

  • http://ruletheweb.co.uk/ Phil

    The match expressions in the <FilesMatch ...> directives all start with an unescaped period, which will match to any character. You really need to put a backslash in front of them so that they only match to periods. For example, instead of
    <filesMatch ".(flv|ico|pdf|avi|mov|ppt|doc|mp3|wmv|wav)$">
    it would be better to say
    <filesMatch "&#092;.(flv|ico|pdf|avi|mov|ppt|doc|mp3|wmv|wav)$">

  • http://www.askapache.com/ AskApache

    @Phil

    Fixed. Thanks I'm not sure what happened, my DB must've gotten messed up at some point..

  • http://kiptis.com Kip

    What methods can be used on shared hosting?

  • Danny A

    cant read this site due to stupid non removable advert hiding the text. very frustrating and distracting and annoying

    • http://arwym.com/ Jessica M. Vázquez Rodríguez

      Use Adblock or similar extension/plug-in.

  • Pingback: CSS-Umbau - starker Trafficverlust - XHTMLforum

  • Pingback: 10 ways to speed up WordPress - thisismyurl.com

  • Pingback: Speed Up Your Site ตอน2 : Modify Htaccess | NoteBook

  • Pingback: mod_expire, cache_limiter, cache_expire 등 브라우저 캐시 관련 | BUYWORKSELL

  • Pingback: html headers caching | 做人要豁達大道

  • Tom

    Very helpful - it actually allowed me to solve the problem of pages being cached that were not supposed to be cached. Thanks!

  • Uttam

    I have Header set Cache-Control "max-age=14400, private" and also "Expires" header with past date. what will be the expected result

    • Michael Eric Menk

      When time since the download of the object is less than max-age, the object is retrieved from cache, regardless of Expires...

  • http://www.nicoblog-games.com/ Nico

    Shouldn't it be between tags for it to work?
    THis code generates internal server error on i add it to htacces

  • http://www.kunius.com/ kuniusreviews

    Thank you for this useful information. It'll really help some of my sites increase in speed! Thanks again!

  • vince

    Thank you very much for the tip. Used it for my website, improved my yslow mark :)

  • http://www.antoniocervone.com/ Antonio Cervone

    Very helpful, thanks!

  • http://www.vacationphonesja.com Jamaica prepaid cell phone

    Thank you very helpful info!

  • Pingback: HTTP-Cache: PageSpeed & Ressourcen - Host Europe Blog

Popular Articles
My Online Tools

Related Articles
Newest Posts
Twitter



Hacking and Hackers

The use of "hacker" to mean "security breaker" is a confusion on the part of the mass media. We hackers refuse to recognize that meaning, and continue using the word to mean someone who loves to program, someone who enjoys playful cleverness, or the combination of the two. See my article, On Hacking.
-- Richard M. Stallman






[hide]

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.

| Google+ | askapache

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

↑ TOPMain