Black Friday50% off every Pro template and membership with code50% off with Ends in 05days15hrs54min17secShop the sale →

How to Add Expires Headers to a WordPress Site

Introduction Are you looking for information or tutorials to help you add expires headers to WordPress? If yes, don’t miss this article since we will give you some information about expires headers as well as how to add them to…

How to add expires headers to Wordpress site

Introduction

Are you looking for information or tutorials to help you add expires headers to WordPress? If yes, don’t miss this article since we will give you some information about expires headers as well as how to add them to your website.

What are expires headers?

Expires headers are defined as rules that let a web browser know whether it should load web page resources from the user’s browser cache or from your server.

When a new visitor accesses a page of your website for the first time, all the files for that page will be loaded one by one. This is the main reason for long website load times. As you know, the browser cache is responsible for storing some or all of those loaded files on the visitor’s computer. And after the first visit, when the visitor returns to your site, those stored files will be loaded from their computer. This improves the performance of your WordPress site.

How can they be used in WordPress?

One question is how the web browser knows which files to store and which not to. And if they are saved, how long will the browser cache keep them? At this point, we need the support of expires headers.

We can use expires headers or cache-control headers to create rules that decide which files should be saved and how long to save them. In today’s blog, we will show you how to use expires headers to set an expiration date for each file type saved in the browser’s cache. After the expiration date, the browser will reload the files with the latest version of the page or website.

There are 2 ways to add the expires headers to your site:

  • Using a plugin
  • Utilizing code

Now, let’s explore the guide!

Step-by-step instructions to add expires headers to a WordPress site

Using W3 Total Cache to add expires headers

If you have WP Rocket installed and activated, you can use this plugin to automatically add expires headers. On the other hand, if you want to use a free plugin to do this, try using W3 Total Cache. In this blog, we will guide you with W3 Total Cache – one of the most useful WordPress Cache Plugins.

Just like any other plugin, the first thing you need to do is install W3 Total Cache. If you have already installed it, be sure to activate it and update it to the latest version.

To install it, on the admin dashboard, select Plugins -> Add New, then enter the name in the search bar. After that, just click the Install Now -> Activate button for the plugin.

add expires headers to wordpress 1

Now, you can set the expires headers by going to Performance -> Brower Cache. In the general section, there is a set expires header option for you to select. Simply tick the box before the option you want.

add expires headers to wordpress 2

Besides the general setting, this plugin also allows you to set the expires header and set the lifetime for CSS & JS, HTML & XML, media, and other files.

add expires headers to wordpress 3

Don’t forget to click on Save All Settings after finishing your changes.

Utilizing code to add expires headers

This method involves adding code to your WordPress files. To keep your site safe, you should back up all your website data using WordPress Backup Plugins or other manual methods. After that, check whether your site is running on an Nginx or Apache server.

To see whether your website uses Apache or Nginx, open your site. Then, right-click and select Inspect.

check site server 1

In the next step, choose Network, don’t forget to press F5 or refresh the page to see the results. After that, type the domain name of your site in the box in the upper left corner.

Now, your domain site will appear in the Name column. Click on it, and a table containing headers, preview, response, initiator, timing, and cookies sections will be shown. You need to select the Headers section, then scroll down and find the server in the Response headers part.

check site server 2

This way, you can determine the web server of your WordPress site.

  • If your web server is Apache, follow the guide below.

With an Apache server, you need to access the .htaccess file, then add the code below near the top of this 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 3 days"
</IfModule>
## EXPIRES HEADER CACHING ##

This code will help you turn on browser caching and allow you to decide how long the loaded files will be stored. These values are pretty good for most sites. However, you can also change the length of time it is stored in the browser if you want. In addition, this code sets different cache expiration dates. Usually, images are cached longer than HTML, JavaScript, CSS, etc.

  • If your site server is Nginx, the instructions below are for you.

Firstly, you need to access the server configuration file and add the following code to add expires headers to WordPress:

location ~* \.(jpg|jpeg|gif|png|svg)$ {
expires 365d;
}


location ~* \.(pdf|css|html|js|swf)$ {
expires 3d;
}

This code sets the expiration times for all kinds of file types. Of course, the expiration times differ between file types.

Wrap up

To sum up, we have recommended 2 methods to add expires headers to a WordPress site. We hope that you can implement both of them successfully. In addition, if you have any questions or want more information, leave a comment below and we will answer you as soon as possible. Last but not least, don’t forget to spend a little time visiting our Free WordPress Themes, which we hope you will find interesting.

Build it with a template

Start from a finished design: Joomla templates, WordPress themes or the AI-ready block themes. Every template has a free version and a live demo.

Stuck on a template?

Pro customers can open a ticket; everyone can read the documentation.

Free download

We'll send the link, setup tips and update notices, and you can reach us anytime through our AI assistant and contact form.

The download starts at once. We email you the link and ask you to confirm your address. Privacy policy.