Do you want to learn how to add ‘expires headers‘ in WordPress to speed up your site? If so, you‘ve come to the right place.
After 15 years as a WordPress expert, I‘m going to walk you through exactly how expires headers work and two easy methods to add them to your site.
By the end, you‘ll have a detailed understanding of how to use expires headers to boost your WordPress performance. Let‘s get started!
Contents
What Are Expires Headers and Why Do They Matter?
When a user first visits your WordPress site, the browser needs to download all of the resources like HTML, images, CSS, JS files, etc. This means lots of requests back and forth between the browser and your hosting server.
All those requests can really slow down your page load times.
Enter browser caching…
Browser caching allows some resources to be stored locally on the user‘s computer so they don‘t need to be re-downloaded each time.
But how does the browser know which files to cache and for how long?
That‘s where expires headers come in!
Expires headers are rules that tell the browser when a file expires and needs to be refreshed from the server.
You can set expires headers for each file type like:
- Images – Cache for 1 year (they rarely change)
- CSS/JS – Cache for 1 month
- HTML – Cache for 1 week
This greatly reduces the number of requests to your server.
According to Google‘s PageSpeed Insights, expires headers can reduce page load times by up to 50%!
Now let‘s look at two easy ways to add them in WordPress.
Method #1: Use a Caching Plugin (Recommended)
The easiest way to add expires headers is by using a WordPress caching plugin like WP Rocket.
Benefits of using a caching plugin:
- Requires no coding or .htaccess edits
- Enables expires headers automatically
- Provides optimal default cache expiry times
- Improves site speed and page load times
- Offers other performance benefits like minification
Plugin | Cost | Pros | Cons |
---|---|---|---|
WP Rocket | $49/year | Beginner-friendly, wide range of features | Paid membership |
W3 Total Cache | Free | Open source, flexible settings | Steeper learning curve |
WP Fastest Cache | Free | Simple setup, works right away | Limited configuration options |
Based on my experience, I recommend WP Rocket because it‘s easy to set up and offers the full suite of features even at the starter price of $49/year.
Once installed, WP Rocket automatically enables expires headers sitewide using optimal defaults. And you can customize the cache expiry times in the settings if needed.
To get started, check out my complete guide on installing and configuring WP Rocket.
Method #2: Add Expires Headers in .htaccess
If you want to manually add expires headers, you can edit your .htaccess file. This works for WordPress sites hosted on Apache servers.
Here are the steps:
-
Connect to your hosting account via FTP and open the .htaccess file in the root folder.
-
Add the following code snippet:
## EXPIRES CACHING ##
<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 week"
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 ##
- Save changes and upload the file.
This will enable browser caching for the major file types with reasonable default expiry times. Feel free to customize the cache periods based on your site‘s needs.
Pro Tip: Make sure to restart Apache after editing .htaccess for the changes to take effect.
Testing and Troubleshooting Expires Headers
To validate that expires headers are working properly, you can use browser developer tools or online tools:
- Chrome DevTools – Open the Network tab and inspect the headers
- PageSpeed Insights – Shows if expires headers are missing
- WebPageTest – Check caching info under the Waterfall tab
Some common issues include:
- Expiry time is too short – Resources expire too quickly
- Expiry time is too long – Outdated resources are served from cache
- Headers are missing – Browser caching isn‘t enabled
Adjust the caching times as needed until you find the optimal balance for your site.
Leveraging CDNs and Other Caching Layers
For improved performance, you can also enable caching on a content delivery network (CDN). Popular CDNs like Cloudflare offer built-in caching.
Stacking a CDN cache on top of your WordPress expires headers allows resources to be stored at multiple levels:
- Browser Cache
- CDN Edge Cache
- Your Server
This means fewer requests all the way to your origin server.
I have seen sites run 2-3x faster by optimizing caching with both expires headers and a CDN.
Final Thoughts
Adding expires headers is a quick and easy way to boost your WordPress performance.
By enabling browser caching, you can drastically cut down on server requests and speed up page load times.
I recommend using a caching plugin like WP Rocket to automatically enable optimized expires headers across your site.
Alternatively, you can manually add expires rules to your .htaccess file – just be sure to customize the expiry times based on your needs.
To take your speed to the next level, pair expires headers with a CDN.
I hope this guide helped you learn how to improve your site‘s performance using expires headers. Let me know if you have any other questions!