Are you looking to Add Expires Headers in WordPress?
Many website speed testing tools like Pingdom and GTMetrix would ask you to add expires headers. It is one of the common WordPress performance optimization suggestions.
You can do it easily and the research has shown it affects the website speed positively. But you have to know what webserver your website is using. Let’s see how expire headers helps, and how you can implement it to your WordPress website.
What are Expires Headers and Why Add them?
Expires headers are the expiration date assigned to the cache files. It tells the web browser whether to download the resource from the web server or use the one saved in the local browser.
The browser saved the website files in the local memory. So when the same assets (like website logo is the same for every page) is requested, browser can fetch it from the cache. The process is faster than getting the files from the server. Hence it improved the website speed and reduced the bandwidth usage.
But the browser should know if it is fine to get the saved file, or if it’s time to get the fresh version. The expires headers set up the time to use the fresh content.
You can set the different time for the different types of file. For the text it could be 3 months, as the text is changed regularly. While the images are not changed that much so they could be cached for 6 months.
How to Fix “Add Expires Headers” in WordPress?
There are three ways to fix ‘Add Expires Headers’ in WordPress. Let’s see.
#1 Add Expires Headers with a WordPress Plugin
Like everything, there is a WordPress plugin to fix Add Expires Headers. Search Add Expires Headers in the WordPress plugin repository.
The Expires Headers plugin certainly works, but its functionality is limited to this task. But as a dedicated plugin, it contains multiple settings related to adding expires headers. For example: You can select the different expiry time for different media files.
If you are already using WP Fastest Cache, W3 Cache, WP Rocket and WP Super Cache, find the expires header setting in those plugins.
#2 Add Expires Headers With .htaccess on Apache Web Server
If your WordPress website host uses an Apache web server, you can add Expires headers by adding code into the .htaccess file. The .htaccess file is in the root folder of the server.
If you have FTP access, follow these steps:
- Connect to the website server using FTP credentials
- Find the .htaccess file in the root folder
- Download the original file as a backup
- Add the below code to the .htaccess file
## EXPIRES HEADER 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 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 2 days"
</IfModule>
## EXPIRES HEADER CACHING ##
The default values are fine to begin with, but you can change them if you wish.
If you don’t have FTP access, you have to go to the hosting panel/control panel to edit the file.
- Login to your website hosting
- Locate the cPanel and open it
- Open the File Manager
- Go to the WordPress root folder
- Download the .htaccess file for the backup
- Edit the file
- Add the code to the file
- Save the changes
#3 Add Expires Headers with config File on Nginx Web Server
If the WordPress hosting is using Nginx server, you have to add the code to the server configuration file. To add the code to the server, you have to contact the hosting team.
At ServerGuy, we do it automatically to every website we host.
The code:
location ~* \.(jpg|jpeg|gif|png|svg)$ {
expires 365d;
}
location ~* \.(pdf|css|html|js|swf)$ {
expires 2d;
}
Test If Expires Headers Are Working
Once the expires headers are added to the WordPress, you should test if it works or not.
Open the GiftOfSpeed Tool
Test your URL and the application will list all the resources and their expiry time.
Another method is to use the curl command. Connect to the server and open the terminal inside the root file. Run the following command on a website asset.
curl -I https://serverguy.com/wp-content/plugins/betterdocs/public/css/betterdocs-public.css
The output will show you the URL information – which contains the expires headers.
Final Words
Adding expires headers is great for website speed. It helps you control the cache of different file formats on your website. Beside, the speed optimization tools lowers the speed score if expires headers are not working, and your client might complain about it. It would be better to do it.
However, if you are on ServerGuy WordPress Hosting, you don’t have to worry about it. We do it automatically, and help the website owners in customizing the timing as they like.
In this quick tutorial, we learned adding expires headers. If you encounter any issue, please feel free to leave it in the comment section.