A frequent mistake made by WordPress site owners is neglecting browser caching. Slow page load times can deter visitors from your website. Even after optimizing images and upgrading hosting, your site may still feel slow until you implement expires headers.
Expires headers instruct visitors’ browsers to cache your site’s files, such as images, CSS, and JavaScript, for a designated period. This means that when a user navigates through multiple pages on your site, their browser doesn’t need to re-download all the files.
This leads to significantly quicker loading times for returning visitors, enhancing user experience and potentially improving your search engine rankings.
We have successfully implemented expires headers on numerous WordPress sites and have streamlined the process into two effective methods that are compatible with any hosting environment. Whether you prefer to edit code or use a plugin, we will guide you on how to add expires headers to your WordPress site.
How Are Expires Headers Implemented in WordPress?
When a user first visits a webpage on your WordPress site, all files are loaded sequentially. These HTTP requests between the browser and your WordPress hosting server contribute to increased webpage loading times.
Browser caching allows some or all files to be stored on a visitor’s computer. This means that when they revisit the page, the files can be quickly loaded from their own device, enhancing the performance of your WordPress website and improving the overall user experience.
You may be curious about how browsers determine which files to cache and for how long. This is where ‘expires headers’ play a crucial role.
The guidelines for which files to cache and their duration are established through either expires headers or cache-control headers. This article will concentrate on expires headers since they are generally easier for most users to implement.
Expires headers define an expiration date for each type of file stored in the browser cache. After this date, the files will be reloaded from your server, ensuring that visitors see the latest version of the page.
We will guide you through adding expires headers using two different methods. The first method is simpler and is recommended for most users. You can click the links below to jump directly to your preferred section:
- Add Expires Headers Using the WP Rocket WordPress Plugin
- Add Expires Headers in WordPress with Custom Code
- Common Questions About Expires Headers in WordPress
- Video Tutorial
Method 1: Implement Expires Headers Using the WP Rocket WordPress Plugin
WP Rocket is the most user-friendly performance plugin for WordPress available today. Once activated, it immediately enhances your website’s speed without the need for complicated configuration, unlike many other caching plugins.
WP Rocket is a premium plugin, and the great news is that all features are included in their most affordable plan.
The first step is to install and activate the WP Rocket plugin. For detailed instructions, refer to our comprehensive guide on how to install a WordPress plugin.
After installation and activation, WP Rocket automatically enables browser caching. By default, it adds expires headers and cache-control headers with optimal settings to enhance the speed of your WordPress site.
That’s all there is to it! If you want to explore more about the plugin, check out our guide on how to properly install and configure WP Rocket in WordPress.
If you prefer a free caching plugin to add expires headers to your website, we suggest considering W3 Total Cache.
W3 Total Cache provides some similar features to WP Rocket, but it is not as user-friendly. Additionally, it does not include all the features found in a comprehensive performance plugin like WP Rocket, and you will need to manually enable expires headers since it does not do so automatically.
Method 2: Adding Expires Headers in WordPress with Code
The second approach to implement expires headers in WordPress involves inserting a code snippet into your WordPress files. This method is not recommended for beginners, as errors in the code can lead to significant issues and potentially crash your site.
Before making any changes, it’s essential to back up your WordPress site. For detailed instructions, refer to our guide on backing up and restoring your WordPress site.
Now, let’s explore how to add expires headers by inserting code into WordPress.
Identify Whether Your Website is Using Apache or Nginx
First, determine if your website is hosted on Apache or Nginx servers. To do this, navigate to your website, right-click on the page, and select the ‘Inspect’ option.
Next, click on the ‘Network’ tab located at the top of the page.
You may need to refresh the page to see the results.
Then, click on your domain name in the ‘Name’ column.
Scroll down to the ‘Response Headers’ section and look for an entry labeled ‘server’.
This will indicate which web server is in use. For example, if it shows Nginx, then your site is running on an Nginx server.
Now that you have identified the web server your site is using, you can use the links below to navigate directly to the relevant sections:
- Adding Expires Headers in Apache
- Adding Expires Headers in Nginx
Implementing Expires Headers in Apache
To implement expires headers on an Apache server, you need to insert specific code into your .htaccess file.
To modify this file, connect to your WordPress hosting account using an FTP client or the file manager provided by your host. You can locate your .htaccess file in the root directory of your website.
Important: Occasionally, the .htaccess file may be hidden. If you are having trouble locating it, refer to our guide on troubleshooting the visibility of the .htaccess file on your WordPress site.
Next, you will need to add expires headers to enable browser caching. This instructs the web browser on how long to retain your website resources before they are cleared.
You can insert the following code towards the beginning of your .htaccess file:
## Caching with Expires Headers ##
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 image/svg "access 1 year"
ExpiresByType text/css "access 1 month"
ExpiresByType application/pdf "access 1 month"
ExpiresByType application/javascript "access 1 month"
ExpiresByType application/x-javascript "access 1 month"
ExpiresByType application/x-shockwave-flash "access 1 month"
ExpiresByType image/x-icon "access 1 year"
ExpiresDefault "access 3 days"
## Caching with Expires Headers ##
These settings are suitable for most websites, but feel free to modify the timeframes to better suit your specific requirements.
The code specifies varying cache expiration times depending on the file type. Images are cached for a longer duration compared to HTML, CSS, JavaScript, and other file formats since they tend to remain unchanged.
Adding Expires Headers in Nginx
If your WordPress blog is hosted on an Nginx server, you will need to modify the server configuration file to implement expires headers.
The method for editing and accessing this file varies by hosting provider, so it’s advisable to contact your host for assistance if you need help accessing it.
Next, you should insert the following code:
location ~* .(jpg|jpeg|gif|png|svg)$ { expires 365d;
}
location ~* .(pdf|css|html|js|swf)$ { expires 3d;
}
This code establishes expiration times for various file types. Note that images are cached for a longer duration compared to HTML, CSS, JS, and other file types, as images typically remain unchanged.
Common Questions About Expires Headers in WordPress
Here are some frequently asked questions regarding the implementation of expires headers on WordPress sites. These answers should help address any concerns you may have about the process.
1. What are expires headers, and why are they important?
Expires headers are directives that inform web browsers how long to keep (cache) your website’s files on a visitor’s device. When someone visits your site, files such as images, CSS, and JavaScript are downloaded and temporarily stored. With expires headers in place, returning visitors won’t need to re-download these files until they expire, enhancing your site’s loading speed.
2. Will adding expires headers disrupt my WordPress site?
When implemented correctly, expires headers will not disrupt your website. However, if you are using manual coding and make an error in your .htaccess file, it could lead to problems. Therefore, we strongly recommend backing up your site before making any changes and using a plugin like WP Rocket for beginners, as it automates the process and minimizes risks.
3. What is the recommended duration for setting expires headers for different file types?
We suggest assigning longer expiration times for files that seldom change, such as images (1 year), and shorter durations for files that are updated more frequently, like CSS and JavaScript (1 month). HTML files should have the shortest cache times (a few days) since your content is updated regularly. The code examples provided in our article utilize these effective timeframes that are suitable for most WordPress sites.
4. Can I implement expires headers with any WordPress hosting provider?
Yes, expires headers are compatible with most hosting providers, but the method of implementation varies based on your server type. Most shared hosting services utilize Apache servers, which means you will need to add code to your .htaccess file. Some hosts operate on Nginx servers, which require modifications to the server configuration file. If you are uncertain about your server type, we explain how to check this in the article, or you can reach out to your hosting provider for assistance.
5. Do I still need expires headers if I’m using a CDN?
CDNs (Content Delivery Networks) enhance your site’s speed by delivering files from servers closer to your visitors, but expires headers offer additional advantages. They complement CDNs by further minimizing server requests and enhancing loading speeds.
Many CDN providers implement their own caching rules, but configuring expires headers on your origin server guarantees uniform caching behavior in all situations.
6. How can I verify if my expires headers are functioning correctly?
You can check the effectiveness of your expires headers using online tools such as GTmetrix, Google PageSpeed Insights, or Pingdom. These platforms will evaluate your site and indicate whether browser caching is set up correctly. Additionally, you can utilize your browser’s developer tools to inspect the response headers and confirm if the expires or cache-control headers are included with your files.
We hope this guide has helped you understand how to implement expires headers in WordPress. You may also want to explore our tutorial on easily resolving the ‘Leverage Browser Caching’ warning in WordPress or check out our expert recommendations for the best WordPress CDN services.
If you enjoyed this article, consider subscribing to our YouTube Channel for informative WordPress video tutorials. You can also connect with us on Twitter and Facebook.



