How to Fix Add Expires Headers in WordPress?

Add Expires Headers in WordPress

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.

How Caching Works
KeyCDN Image

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. 

Fix Add Expires Headers in WordPress

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.

Fix Add Expires Headers in WordPress

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:

  1. Connect to the website server using FTP credentials
  2. Find the .htaccess file in the root folder
  3. Download the original file as a backup
  4. 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.

  1. Login to your website hosting
  2. Locate the cPanel and open it
  3. Open the File Manager
  4. Go to the WordPress root folder
Find Htaccess file on WordPress
  1. Download the .htaccess file for the backup
  2. Edit the file
  3. Add the code to the file
  4. 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

Expires Headers Checker 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.

Latest WordPress Tips, Guides, & News

Stay updated with new stuff in the WP ecosystem including exclusive deals, how-to articles, new plugins, and more. 100% WordPress Goodness, a promise!

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top

We can help you. Right now.

Fast growing merchants depend ServerGuy for high-performance hosting. Experience counts. Let's get started.

Talk to a sales representative

USA / Worldwide

+1.714.2425683

India

+91.9852704704

Core Web Vitals Book COver

Is your website ready for Core Web Vitals?

Take this FREE book with you and optimize your store for speed.

Learn all about new Google new ranking factors and get that top ranking.