How to add Expires Headers in WordPress? An Easy Guide
Adding expires headers is a great way to increase your website’s load speed. We have shown you various methods through which you can add expires headers in your WordPress website.
Expires headers let browsers know if they should fetch a resource on a website from the browser’s cache or if it needs to be requested from the source e.g database. For example, is expires headers are set for images when a user loads a website, the browser will store the images in the cache. The next time the visitor comes back, the page will load much faster, as the images required are not requested from the database but are instead loaded from the browser’s cache.
Expires headers can be added to specific files or even file types. When a visitor revisits a website, the browser will check when was the last time the resource was downloaded. If it was recently, then it will display those from the cache. Otherwise, it will download a newer version from the server. It is, therefore, best practice to set long expiry times for items that don’t change on your website for example logo, colors, header pics, footer icons, etc. You can also set short expiry times for items that change frequently.
This ultimately influences the website’s speed. It improves the user experience, increases average time spent and helps lower the bounce rate. Search engines use page speed as a major ranking factor when ranking websites in search engines. Adding expires headers can also help reduce bandwidth costs by reducing the number of times an image has to be requested from the database.
SPEED TEST TOOLS
Often when running speed tests with tools such as GTMetrix you will get a recommendation to add expired headers. An example is shown below:
Other tools such as Google PageSpeed, Pingdom and many other speed test tools recommend implementing expires headers.
You may be wondering how long should you set expires headers for? Well, there are no hard and fast rules for that. You need to analyze your situation. As a general rule for files that are modified frequently, set shorter expiry time. For files such as your logo, you can set an expiry time of 1 year.
Adding Expires Headers in WordPress
There are several methods of adding expires headers in WordPress. You can use a plugin or manually add code to your .htaccess file.
Adding Expires Headers Using a Plugin
Install and activate the Add Expires Header Plugin.
Check Files types you want to have expires headers and add respective expires days for mime type using input box and make sure you enable respective mime type, for which group of files you want to add expires headers.
Once you hit the “submit” button, all options you selected on the settings page are saved in the database of the website. The .htaccess file is updated accordingly and the expires headers are added to the respective selected files.
If you are using a cache plugin such as W3 Total Cache, WP Rocket or any other similar cache plugin, you might already have set up expires headers. You, therefore, don’t need a separate plugin to add expires headers.
This plugin is a simple and easy to use solution for anyone not looking to fiddle with code on their website. However, it is also advisable to avoid using plugins wherever possible. Often plugins will slow down your website and add bloated code to it. They may also have compatibility issues with other plugins or your theme. Therefore if you are in such a situation it is recommended to manually add expires headers.
Manually Adding Expires Headers to Apache Servers
This method may not work for all web servers and may differ slightly depending on if you are using an apache server or an Nginx server. The general process, however, is the same.
To access the .htaccess file log in to your cPanel, open the file manager.
In the file manager navigate to the public_html folder
In the public_html folder you should be able to access your .htaccess file.
If you cannot see the .htaccess file, you need to enable the hidden file view. To do that in the file manager, in the top right-hand corner click on Settings, check Show Hidden Files (dotfiles) option and click on Save.
To edit the .htaccess file, right click it and click on edit as shown below
Copy and paste the following code in your .htaccess file
Manually Adding Expires Headers to Nginx Servers
If you are using an Nginx server they don’t have a .htaccess file. You need to edit the server block. Copy and page the following code to the server block.
Using expires headers is a great way to increase your website’s load speed. We have shown you various methods through which you can add expires headers in your WordPress website. We hope you found the article helpful and are now easily able to add expires headers.