How to leverage browser caching using .htaccess

I was using Google PageSpeed to optimize a website and got a warning telling me to leverage browser caching. On Google’s Optimize Cache page I didn’t see any instructions for how to do this though. After a little searching I found the following .htaccess code on Premium Press.

Apparently, there are certain security considerations that should be kept in mind according Apache SF.

[cc lang=”bash”]
###########HTTP HEADER START###############
# 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
Header append Cache-Control “public”

# Set up 2 Hour caching on commonly updated files

ExpiresDefault A604800
Header append Cache-Control “proxy-revalidate”

# Force no caching for dynamic files

ExpiresActive Off
Header set Cache-Control “private, no-cache, no-store, proxy-revalidate, no-transform”
Header set Pragma “no-cache”

###########HTTP HEADER END###############
[/cc]

Tagged with: , , ,
4 comments on “How to leverage browser caching using .htaccess
  1. Erick P says:

    Thank you! It worked perfectly on my website!

  2. Bryan says:

    Not working for me,
    Using google pagespeed is 60/100.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.