How to Add Expires Headers for WordPress

🖊 Written by Updated on

The Expires Headers of a website tell the browser when to load its resource from the server or its cache.

When you build a WordPress website on a VPS or Cloud server, you need to configure expires headers on your server manually. So, your website runs smoothly as electricity.

If you haven’t configured expires headers on your site, you need to do it now. It affects very badly on site performance.

When we set up our website on the google cloud platform and check-up the GTMetrix or Google Page Speed Insight performance test, we got some ‘F’ grade results, as showing below images.

GTMetrix Performance Score:

Expires headers score on Gtmetrix
Gtmetrix Speed Insight

Google Pagespeed Insight Score:

Expires Header Score Pagespeed insight

As you can see, both of the speed test results. Expires headers affect a terrible impact on website performance. At present, site performance is one of the most important SEO factors for Google search engine.

So now, we are going tweaking our WordPress VM instances on Google Cloud step by step.

Connect Compute Engine with SSH

First, we will connect our VM instances or Compute engine through SSH, and then we will use some command for adding expires headers. We will note all commands and certainly down with steps.

Now, (After connecting VM with SSH), we are going to execute a command for editing ‘httpd.conf‘, the command looks like this:

sudo nano /opt/bitnami/apache2/conf/httpd.conf
linux command vm gcp

Enable Expires Headers

In this step, we will search the expire headers modules, now click CTRL+W, then type “expires” in the search box and hit ENTER. Now a list will open like the picture shown below.

linux command vm gcp
linux command vm gcp

You can see the red box, we going to remove the hash (#) sign like this:

{#LoadModule expires_module modules/} to {LoadModule expires_module modules/}

Adding Expires Headers

After enabling the expires headers modules we will add more information about the expires headers by a command

linux command vm gcp command
sudo nano /opt/bitnami/apps/wordpress/conf/httpd-app.conf

After using this command, there will open a page shown in the picture below. We will add some expires headers text.

linux command vm gcp edditing httdocs

We should add the text between the tag:

</IfModule> and <IFDefine USE_PHP_FPM>
linux command vm gcp adding expires headers

The text looks like this the picture shown above, or you can copy the text from below.

<filesMatch ".(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf)$">
Header set Cache-Control "max-age=2592000, public"

<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 text/css "access 1 month"
ExpiresByType application/pdf "access 1 month"
ExpiresByType text/x-javascript "access 1 month"
ExpiresByType application/x-shockwave-flash "access 1 month"
ExpiresByType image/x-icon "access 1 year"
ExpiresDefault "access 2 days"
linux command vm gcp restart apache

Now restart apache server by command.

sudo /opt/bitnami/ restart apache

Test Performance

Now everything is done, so we will test the performance of the site. We will use GTmetrix.

performance report apache

The performance is not so great, but it is better than first. Before adding the expires headers module, we get 0 out of 100 and now about 45 out of 100.

If you want to learn how to set up your website and domain name on the Google cloud platform, click here to read the article.


Expires headers misconfiguration is very bad for your WordPress website. So you can follow these steps to configure expires headers correctly on your server.

If you do not want to configure through CLI, you should use a plugin named HTTP headers. This plugin is very easy to configure, and you can also configure other headers policies.

If you have any problems or suggestions about expires headers, then throw your comment in the comment section.

Please Share & Support ♥

I am a blogger and a digital marketer based in Delhi, India. I have a passion for blogging and influencing people from my work. I am a student pursuing a Bachelor's Degree in Science from Delhi University.

Leave a Comment