How to Add Expires Headers in WordPress

Expires headers tell the browser to whether to serve cached versions of a webpage by defining the expiry time. It helps to –

  • Reduce server load
  • Reduce requests
  • Increase page load speed

What are expires headers

Add-Expiry-Headers

Expires header tells the browser whether they should request to download the same file from the server or grab it from the cache data from the browser.

Its main purpose is to reduce the HTTP request on the server and prevent the same file from being downloaded continuously.

When a user visits a website, the browser requests the server to download all the required files of the webpage. The browser sends all the data to the browser, including HTML, CSS, JS, and Images files. After displaying all the files, the browser saves those files in users’ devices as cache data.

When that user visits that page again, the browser does not request the server to download all those files back. And instead of downloading, it already shows the saved files as cache data in the browser.
This reduces the request on the server, thus reducing the load on the server.

How does expires headers work?

The function of the expire header is simple. They tell the browser how long they have to store the webpage’s files as cache data, and when to request the server to download the new files.
You can specify how long the files will save in the browser. It does not improve load time for the first time but reduces the load time of next time.

How to Add Expires Headers

Here are two methods of add Expire header to website. The first method is for WordPress user with a plugin and the second method is for all types of website.

Add Expires Headers in WordPress

This is a very easy method to add Expires header. Just install the ‘Far Future Expiry Header‘ plugin to WordPress and activate this. How you do this? Follow these steps-

  1. Go to WordPress dashboard
  2. Click on Add New in the plugin section
  3. Search Far Future Expiry Header and install
  4. Install and activate the plugin
  5. Go to the FarFutureExpiry option in the Setting option.
  6. Click on ‘Enable Far Future Expiration’.
  7. Enter 30 days in the number of days option

Now save the settings and clear all the cache.

Configuring Apache to serve the Expires Header (General Method)

In this method, we need to insert a code in the htaccess file. Remember that before you edit the htaccess file, you have to back-up this.
Access your website via FTP. Find the htaccess file and open it. In the htaccess file, add this following code and save it.

<IfModule mod_expires.c>
  ExpiresActive On

  # Images
  ExpiresByType image/jpeg "access plus 1 month"
  ExpiresByType image/gif "access plus 1 month"
  ExpiresByType image/png "access plus 1 month"
  ExpiresByType image/webp "access plus 1 month"
  ExpiresByType image/svg+xml "access plus 1 month"
  ExpiresByType image/x-icon "access plus 1 month"

  # Video
  ExpiresByType video/mp4 "access plus 1 month"
  ExpiresByType video/mpeg "access plus 1 month"

  # CSS, JavaScript
  ExpiresByType text/css "access plus 1 month"
  ExpiresByType text/javascript "access plus 1 month"
  ExpiresByType application/javascript "access plus 1 month"

  # Others
  ExpiresByType application/pdf "access plus 1 month"
  ExpiresByType application/x-shockwave-flash "access plus 1 month"
</IfModule>

You can modify the expires time by changing the time in the above-given code, but the 30-days is the minimum value of it(recommended: access plus 1 year).

YOU MAY READ: 5 Useful Blogging Tools for Writing and SEO

If you like this tutorial “How to Add Expires Headers” than don’t forget to share your views in comment.

2 Comments

Leave a Reply

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