How to Leverage Browser Caching in WordPress Without a Plugin

WpFASTER Code 11 Comments

Are you getting a “Leverage Browser Caching” recommendation in PageSpeed Insights, GTmetrix or Pingdom (etc)? No need to add another plugin just for this! To leverage browser caching in WordPress, copy and paste this code snippet — in plain text — to the top of your .HTACCESS file:

## Begin EXPIRES CACHING - WpFASTER ##
<IfModule mod_expires.c>
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 text/html "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 1 month"
</IfModule>
## End EXPIRES CACHING - WpFASTER ##

Not working? Try this snippet instead:

## Begin EXPIRES CACHING - WpFASTER ##
<filesMatch ".(css|jpg|jpeg|png|gif|js|ico|woff|woff2|ttf)$">
Header set Cache-Control "max-age=2592000, public"
</filesMatch>
## End EXPIRES CACHING - WpFASTER ##

Do you use Cloudflare? You can bypass messing with your .HTACCESS file altogether and leverage browser caching for WordPress using the following two Page Rules:

Cloudflare Page Rule for wp-content:

  1. Enter your domain name with an asterisk in front of it (no www., no http or https, etc)
  2. Add “/wp-content/*” (no quotes) to the end of your URL.
  3. Click “Add a Setting” and select “Browser Cache TTL” and select “a year”.
  4. Click “Add a Setting” and select “Edge Cache TTL” and select “a month”.
  5. Click “Add a Setting” and select “Cache Level” and select “Cache Everything”.
  6. Click “Save and Deploy”.

Visual:


Cloudflare Page Rule for wp-includes:

  1. Enter your domain name with an asterisk in front of it (no www., no http or https, etc)
  2. Add “/wp-includes/*” (no quotes) to the end of your URL.
  3. Click “Add a Setting” and select “Browser Cache TTL” and select “a year”.
  4. Click “Add a Setting” and select “Edge Cache TTL” and select “a month”.
  5. Click “Add a Setting” and select “Cache Level” and select “Cache Everything”.
  6. Click “Save and Deploy”.

Visual:

Enjoy!

Comments 11

  1. Your site is really a premium resource to improve site speed. Definitely. How do you handle recommendations to calls to external scripts like google maps, hotjar, facebook, tagmanager, etc. I mean I can’t change the settings on other servers’.

    1. Post
      Author

      Hello Cesare and thanks for the comment!

      Our objective for our clients is optimal real-world performance, not necessarily better grades on best practices tools (the latter being, far more often than not, the only improvement one will get by altering the expiration time of assets not served from one’s own server).

      The important thing is using the asynchronous version of whatever external resources one has implemented, whenever an asynchronous version is available, to insure that those external resources are not interfering with the key user-centric metrics that combine to form perceived speed (see our homepage under “And! — We Give Google EXACTLY What Google Wants” for these metrics).

      If there’s anything we consistently see regardless of how we explain this, however, it’s that people still obsess about grades on best practices tools; so, if you’re gung-ho about those you might try playing around with the proxy feature in the Page Speed Optimization plugin.

      I hope I have been of some help 🙂

      Looking forward!
      Sam D.
      Senior Client Support Representative & Project Management Lead

      1. Hi Sam,
        Thanks for your extensive reply. You have helped me. Sure. I like that you focus on the really relevant things concerning UX. I still haven’t found out 100% how to be able to improve the key user-centric metrics you mentioned but I am trying…

        Thanks again.

        Cheers,
        Cesare

        1. Post
          Author

          It is a journey, for sure. Especially sorting-through all of the dis- and mis-information that exists on the Internet.

          If you are interested, Cesare, we do have a course. Take it front front to back and you will be taught, by default, how to optimize for the user-centric performance metrics that matter the most to real-world performance.

          Looking forward!
          Sam D.
          Senior Client Support Representative & Project Management Lead
          WpFASTER

  2. I implemented the 2 Cloudflare page rules, flushed the cache through Cloudflare, and no change in my results for gtmetrix.com or webpagetest.org?

  3. How can i cache google cdn images. I am using a plugin and in webpagetest website I see few google images are coming (using google review plugin in wp)
    But these are not cached and becoming render blocker.

Leave a Reply

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