How To Add Expire Headers WordPress On Your Website In 2024

How To Add Expire Headers WordPress On Your Website In 2024

Introduction:

Have you recently utilized a performance test tool for WordPress and received a message stating that you must Add Expire headers WordPress website using WordPress?

You can use browser cookies to speed up the load times of your website by using expiration headers. Furthermore, they influence your performance "score" in programs like GTmetrix since they are a component of YSlow's efficiency ideas.

We'll define expiration headers and discuss their impact on your website in this post. Next, we'll walk you through adding expiration headers to WordPress using several different methods, step-by-step. But before we do that, let's quickly digress and discuss browser caching. 

What is a Browser Cache?

We must first expose you to the idea of browser caching before we can discuss expiration headers. This is significant since expires headers enable you to regulate and apply browser caching; in essence, you are unable to understand expires flags unless you're comfortable with browser caching.

Browser caching, in its most basic sense, enables your internet presence to instruct a user's browser to cache certain files on the consumer's local machine and load those media files from memory on subsequent visits, as opposed to downloading them directly from your server each time a page loads.

You can save bandwidth usage and improve site load times by doing away with the requirement of downloading the file every time.

Let's examine your website's logo image as a case study. Since your logo appears on every page, it makes no sense to let visitors' browsers download it again every time a page loads. Instead, you might store the emblem file locally on visitors' PCs using browser caching or add expire headers WordPress. On their initial visit, a visitor would nevertheless be required to download your logo picture. However, the file would load from their local cache in their browser for any further page views.

What Are the Expire Headers?

A visitor's web browser can be informed via expiration headers as to whether a particular resource should be retrieved from the local browser's cache (as previously mentioned) or if a fresh version has to be downloaded from the web server.

More precisely, it allows you to decide the length of time the cached versions of certain file formats will last up until the browser has to download them from the website again when they "expire."

Let's examine an illustration:

Assume you wish to manage the way that your website's PNG image files are cached by browsers. If the Expires directives for PNG files are set to one month, the following occurs in the visitor's browser:

  • After the first download and access, load the previously acquired version from the file cache for a month.
  • After a month, download that file again from the server.
  • You have extremely fine control over the WordPress browser caching of your website since you can define different expiration directives for various file types.

Headers Expire vs. Controlled Cache:

There are other ways to manage browser cache on WordPress besides use Expires headers. Cache control is another method that is available.

A relatively recent technique that offers a little greater adaptability for managing caching behavior is called cache control. Due to this, a lot of websites use cache-control these days, notably Kinsta with its own distinctive Nginx setup.

Expires headers are still a good alternative for browser caching, nevertheless, as they still provide all the features that most websites need. Additionally, you can use both, but in most cases, your cache-control directives will take priority. Make sure to specify the same time intervals in each one if you plan to use both.

How to Correct Add Expire Headers WordPress Issue?

Let's go on to the how-to section, wherein we'll walk you through configuring expires directives in WordPress.

First, though, there is good news. You don't have to bother about setting up expiration tags when you host because we take care of it automatically. You are lacking the ability the ability to do anything because we pre-configure our computer systems and integrated content transmission network (CDN) to take advantage of browser caching.

For your WordPress website, you can apply expiration headers when you're hosting it somewhere. You may achieve this by either using certain user-friendly WordPress plugins or simply by utilizing a custom code snippet at the system level (Apache and Nginx).

How to Use.htaccess to Add Expire Headers WordPress to an Apache Web Server?

The.htaccess file, which is kept in the main directory of your servers (the same directory that contains the wp-config.php file), can be used to add expiration headers if your hosting provider utilizes the Apache web server.

This is what you must do:

  • Make an FTP connection to your server using your favorite FTP program.
  • Look in the main folder for the.htaccess file.
  • To the local machine, download an extra copy of the.htaccess file. In this manner, you can restore a backup copy and deal with any possible issues in case anything goes wrong.
  • Put the following code snippet just below the top of your file.

## EXPIRES HEADER CACHING ##
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access 1 year"
ExpiresByType image/jpeg "Access for 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 lengths of time for the various file types can be modified in the code snippet to suit your particular requirements; however, the default numbers should be adequate for the majority of web pages.

    How to Use a Config File to Add Expiration Headers to a Nginx Web Server?

    You can modify your server's setup file to manage expiration headers if your host runs on a Nginx web server. Your host's editing guidelines will apply to this file; in the event that you require help, please contact their support. 

    Nginx is the web server that Kinsta uses, but keep in mind that we've already configured everything for you, so you won't need to enter any code yourself. This is the code that you must apply: 

    location ~*  \.(jpg|jpeg|gif|png|svg) $

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

    {
    expires 2d;

      The expiration dates for various file types can be modified as needed. 

      How to Utilize a Plugin to Add Expire Headers WordPress?

      There are WordPress plugins that can add this expiration header code if you don't feel comfortable doing it yourself. While some of the above plugins employ cache control, others add expiration signals. The net effect is that browser cache can help your website with WordPress. 

      If your host hasn't done the caching for you yet, a particular way to fix the issue is to utilize a caching plugin. There is no need to set up any additional settings because browser caching is enabled directly as soon as you decide to enable the WP Rocket plugin.

      The following caching plugins can assist users with setting up web browser caches as well as expiration headers: 

      • WP Fastest Cache
      • WP Super Cache 
      • W3 Total Cache 

      Two excellent free plugins should be considered when you've already figured out caching and just want something more focused: 

      • Take advantage of browser caching.
      • Add Expires Headers 

      There are no settings for the Leverage Browser Storing plugin; the only thing you need to do is activate it to get it working properly. 

      You can handle expiration tags for different kinds of files using the Add Expires Tags plugin. The Add Ends Headers tab in the WordPress control panel allows customers to make choices such as these:

      How to Check If Expires Headers Are Working?

      You may utilize this GiftOfSpeed free tool to check when your expiration tags are set up properly. The program will inform you of two things after you enter the address of your website: 

      The time that all of the files on your website are cached. The cache type (because one used this approach, it ought to indicate expired headers; nevertheless, depending on how your site is configured, you could also see cache-control).

      Additionally, you can use any kind of performance test tool you're using (like GTmetrix) to see when the add expire headers WordPress notice is still appearing on your website.

      1. Using Terminal to Test Expires Headers: 

      If you would rather test expiration protocols in a more technical manner, you can use Terminal to make a curl request to a dynamic asset on your website and examine the HTTP headers straight. For instance, you can submit a curl request similar to the one below when your ends or cache-control rule includes CSS files.

      curl -I  https://kinstalife.com/wp-includes/css/dist/block-library/style.min.css

      The response that we obtain from the curl query regarding the style.min.css file is as follows:.  The answer has expired and cache-control headers, as can be seen. In particular, a maximum age of 315360000 minutes is displayed in the cache-control header, indicating a one-year expiration date.  

      2. Add Expire Headers WordPress to External Scripts:

      You may manage the end headers for any file on the server hosting your WordPress website by using the methods we have shown above. But such techniques don't give you any control over the expiration headers of any third-party programs you could be loading, like Google Analytics, Google Fonts, and so on. 

      For this reason, even when you've previously utilized the aforementioned techniques to incorporate Expires headings, some performance test programs may still identify this as a possible problem. These are a few fixes for popular third-party scripts. 

      How to Use Google Fonts' Expires Headers?

      For custom fonts, a lot of websites built with WordPress use Google Fonts. You can't configure the Expires headers through your server's configuration since this usually requires loading those fonts from Google's CDN. 

      Hosting the font files locally rather than using Google's CDN is one simple solution. Use WordPress.org's Optimized My Fonts by Google (OMGF) plugin, which is free, to configure this. 

      Conclusion:

      In summary, when you add expire headers WordPress, it enable administrators to manage how your WordPress website's browser caches various file formats. Expired headers are no longer as common in websites; instead, cache-control headers are a more recent choice that offer greater flexibility. Nevertheless, most websites still function properly with expire headers, and you can utilize both expire elements and cache control simultaneously.  When you don't use expiration headers, some performance test programs will lower your site's score, which may be your primary motivation for putting this strategy in place.

      Premium WordPress templates may offer various optimization features. However, many templates are built with code that is optimized for performance and compatible with popular caching plugins that can add Expire Header functionality. By choosing a performance-focused theme and leveraging a caching plugin, you can effectively set expiration times for static files, reduce server load, and improve overall website performance.

      Back to blog