Expires Headers are certain lines of code that tell your browser how long it should keep the cached files from your site. You can add Expires Headers by adding a code such as ExpiresByType image/jpg “access plus 1 month” to your site. You can find other forms of it below.
One of the most common errors we face when speed testing our websites on GTmetrix is ‘add Expires headers’.
When a user enters your website they will automatically download some files. Each of these files has an expiration date and they stay for a certain amount of time in your cache.
When the time is too low you will face ‘your server is not using expires header for your images.’ Here is how to add expires headers to .htaccess file.
What is Expires Header?
When a user visits a website, the browser will automatically load files and save them in the memory. So, when the user decides to check other web pages of the same website, there is no need to reload all the files.
Expires headers give the command to your browser to download and store these files.
The reason behind this is to reduce the send and receive between your computer and the server (website) and use the data stored in your cache.
Whenever a new user enters your website, all of these files are requested from your server.
Each request takes time. But when files are stored in the memory the number of requests will decrease automatically. Your website will load faster and the user’s bandwidth won’t be wasted.
But the question is, how long should the data stay in the browser’s cache? In order to prevent users from re-downloading shared files between pages, an expiration date should be defined for them.
The defined date tells browsers which file to request from the server and which file to request from the cache.
What files are saved in the cache?
First, you need to identify which files you frequently update and which one you do not. Here is a list of said files:
- Jpg, jpeg, gif, and png
Now it’s time to add expires headers to .htaccess file to avoid getting “add expires headers” error.
Add expires headers to .htaccess file
To add expires headers to WordPress without plugin, you need to use the .htaccess file.
.htaccess is often a hidden file found in your root directory. Before you make any changes to it, make sure you have an extra copy.
Now go to your website’s root and download your .htaccess file, if this file is hidden for you, you may have to use FTP to enable the “Show Hidden files” option.
Once you have downloaded the file, open your .htaccess in any editor (notepad or notepad++) and add the code below:
First, we need to enable “expirations” file. Then we will add a different line for different files. Now go back to the code and change the date however it fits your website.
As a default, we have set this time for your files to be stored on browser’s cache to 1 month, 1 year and 2 days.
After a month, if the user visits your website again, their browser will request these files. After making your changes to .htaccess file make sure you save it and upload it back to your website’s root.
How long the time should be?
The duration of the cache can be set in years, months, weeks, days, hours, and even seconds.
Your expiration date shouldn’t be unrealistic. The expiration date of each file should be set based on the amount of time it’s being used/requested. If you plan to store your files in your browser’s cache for more than a year, we recommend you to optimize your browser.
The expiration date of images can be set to one year since they are not easily changed and they take the most time to load.