add expires headers wordpress

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. 

Also read: 

How to duplicate a Page in WordPress?

How to edit Footer in WordPress

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:

wordpress add expires headers

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.

Also read: How to duplicate a Page in WordPress?

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.

https://wordpress.org/plugins/add-expires-headers/

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.

add expires headers wordpress

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.

add expires headers wp

In the file manager navigate to the public_html folder

wp add expires headers

In the public_html folder you should be able to access your .htaccess file.

expires headers

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.

add expires headers

To edit the .htaccess file, right click it and click on edit as shown below

wp expires headers

Copy and paste the following code in your .htaccess file

<IfModule mod_expires.c>

ExpiresActive On

# Images

ExpiresByType image/jpeg “access plus 1 year”

ExpiresByType image/gif “access plus 1 year”

ExpiresByType image/png “access plus 1 year

ExpiresByType image/webp “access plus 1 year”

ExpiresByType image/svg+xml “access plus 1 year”

ExpiresByType image/x-icon “access plus 1 year”

# Video

ExpiresByType video/mp4 “access plus 1 year”

ExpiresByType video/mpeg “access plus 1 year”

# 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>

In the above code we have set the images and videos to have an expiration time of 1 year. For Javascript and CSS files they will be cached for 1 month. You can change these values depending on your requirements.

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.

location ~* \.(js|css|png|jpg|jpeg|gif|svg|ico)$ {

expires 30d;

add_header Cache-Control “public, no-transform”;

}

CONCLUSION

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.

Leave a Reply