How to Add Expires Headers for WordPress ?

🖊 Written by • ⌚ Updated on

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

If you want to learn how to set up your website or domain name on Google cloud hosting, then you can read our articles about how to add domain name on google cloud. Also, you can get free $300 for one year trial. Click here to read the article.

Performance score gtmetrix prowebtips
Gtmetrix Speed Insight

Figure GTMetrix Results

google pagespeed insight prowebtips

Figure Google Page Speed Insight results

We get the cause of this result of Expires Headers on our website. That also can affect our ranking on google.

So now, we are going tweaking our WordPress VM instances in 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:

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, so we will click CTRL+W, then search by writing “expires” 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 Hound (#) 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.

Thank You!

Recommended Articles


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

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

Share via
Copy link
Powered by Social Snap