Leverage browser caching

16,479

Solution 1

Caching is controlled via a variety of HTTP headers. You should read Mark Nottingham's Caching Tutorial for Web Authors and Webmasters. You can set HTTP headers for documents outputted from PHP using the header function.

Solution 2

You can do something like this in your .htaccess.

  ## EXPIRES CACHING ##
<IfModule mod_expires>
ExpiresActive On
ExpiresByType image/jpg "access 1 year"
ExpiresByType image/jpeg "access 1 year"
ExpiresByType image/gif "access 1 year"
ExpiresByType image/png "access 1 year"
ExpiresByType text/css "access 1 month"
ExpiresByType application/pdf "access 1 month"
ExpiresByType text/x-javascript "access 1 month"
ExpiresByType application/x-shockwave-flash "access 1 month"
ExpiresByType image/x-icon "access 1 year"
ExpiresDefault "access 2 days"
</IfModule>
  ## EXPIRES CACHING ##
Share:
16,479
oshirowanen
Author by

oshirowanen

Updated on June 07, 2022

Comments

  • oshirowanen
    oshirowanen almost 2 years

    According to: http://code.google.com/speed/page-speed/docs/caching.html#LeverageBrowserCaching I should be using browser caching. However, I don't know how.

    Do I simply add certain tags into the html section? Or is thing something I need to send via to server to the client? something to do with php headers?

  • alex
    alex about 10 years
    this doesn't make a difference? i am checking with pingdom.com any other suggestions?
  • Nisse Engström
    Nisse Engström almost 8 years
    @Rikesh: Is the code above quoted from somewhere or is it your own code? I originally though it was copy/pasted from the same place as Ashraf Malik's answer, but I see now that yours was posted years earlier. Did Ashraf plagiarise your code?