How to: Add Expires Headers in WordPress using .htaccess?

Well, the expires headers tell the browsers how often they should request a cached file from the server and till they should grab the old cached file from browser’s cache. The expires headers isn’t only the reduction in the downloads of the files but also it helps browsers to reduce least server requests to which a website can absorb because downloading the same file again and again gonna be wastage of precious load time. So, the browser is responsible for communicating the webserver and download the required files in order to display the content on a webpage. For a first time visit the browser would have to download all the files, so you got correct that expires headers don’t improve page speed but it is a way of reducing the server requests. Though, for the second time visit the expires headers helps you to decrease the page load time.

You add the expires headers manually as well as by using a caching plugin too. Here is what I want to show you that W3 Total Cache is the one which enable you to add expires headers automatically on one click action.

Expires Headers in htaccess file

Adding Expires Headers in WordPress

At first you have to decide what files you update often and also some files don’t get updated often. Here is a common list of file types that we realize so:

  • images: jpg, gif, png
  • favicon/ico
  • javascript
  • css

Add the following code in your .htaccess file, you’ll be able find that file in your root directory  along with WordPress core files.

<IfModule mod_expires.c>
# Enable expirations
ExpiresActive On
# Default directive
ExpiresDefault "access plus 1 month"
# My favicon
ExpiresByType image/x-icon "access plus 1 year"
# Images
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType image/jpg "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 month"
# CSS
ExpiresByType text/css "access 1 month"
# Javascript
ExpiresByType application/javascript "access plus 1 year"
</IfModule>

According to W3 Total Cache plugin a complete pack of expires headers as follows:

<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresByType text/css A31536000
    ExpiresByType application/x-javascript A31536000
    ExpiresByType text/x-component A31536000
    ExpiresByType text/html A3600
    ExpiresByType text/richtext A3600
    ExpiresByType image/svg+xml A3600
    ExpiresByType text/plain A3600
    ExpiresByType text/xsd A3600
    ExpiresByType text/xsl A3600
    ExpiresByType text/xml A3600
    ExpiresByType video/asf A31536000
    ExpiresByType video/avi A31536000
    ExpiresByType image/bmp A31536000
    ExpiresByType application/java A31536000
    ExpiresByType video/divx A31536000
    ExpiresByType application/msword A31536000
    ExpiresByType application/vnd.ms-fontobject A31536000
    ExpiresByType application/x-msdownload A31536000
    ExpiresByType image/gif A31536000
    ExpiresByType application/x-gzip A31536000
    ExpiresByType image/x-icon A31536000
    ExpiresByType image/jpeg A31536000
    ExpiresByType application/vnd.ms-access A31536000
    ExpiresByType audio/midi A31536000
    ExpiresByType video/quicktime A31536000
    ExpiresByType audio/mpeg A31536000
    ExpiresByType video/mp4 A31536000
    ExpiresByType video/mpeg A31536000
    ExpiresByType application/vnd.ms-project A31536000
    ExpiresByType application/x-font-otf A31536000
    ExpiresByType application/vnd.oasis.opendocument.database A31536000
    ExpiresByType application/vnd.oasis.opendocument.chart A31536000
    ExpiresByType application/vnd.oasis.opendocument.formula A31536000
    ExpiresByType application/vnd.oasis.opendocument.graphics A31536000
    ExpiresByType application/vnd.oasis.opendocument.presentation A31536000
    ExpiresByType application/vnd.oasis.opendocument.spreadsheet A31536000
    ExpiresByType application/vnd.oasis.opendocument.text A31536000
    ExpiresByType audio/ogg A31536000
    ExpiresByType application/pdf A31536000
    ExpiresByType image/png A31536000
    ExpiresByType application/vnd.ms-powerpoint A31536000
    ExpiresByType audio/x-realaudio A31536000
    ExpiresByType image/svg+xml A31536000
    ExpiresByType application/x-shockwave-flash A31536000
    ExpiresByType application/x-tar A31536000
    ExpiresByType image/tiff A31536000
    ExpiresByType application/x-font-ttf A31536000
    ExpiresByType audio/wav A31536000
    ExpiresByType audio/wma A31536000
    ExpiresByType application/vnd.ms-write A31536000
    ExpiresByType application/vnd.ms-excel A31536000
    ExpiresByType application/zip A31536000
</IfModule>
Related  How to add Currency Converter Widget in WordPress

11 Comments

  1. Hi Jasa,

    I tried to use this code, but gtmetrix did not detect your Add Expires headers!!!

    Please help!

  2. I tried to use this code, but gtmetrix did not detect your Add Expires headers!
    Please help! any other solution are available if plz share it.

  3. .htaccess method doesn’t work for me. Can you please let me know why?
    Even after adding codes to .htaccess file, the same issue for leverage browser caching shows.
    Please help me out.

  4. I try to do with your tutorial. But when I test on pagespeed the error still occur (expiration not specified).

    Could you please help me? maybe I have to do in mod server to enable the expiration? or just do it on .htacces file.

    *confused

  5. ExpiresByType image/png A2592000
    ExpiresByType image/gif A2592000
    ExpiresByType image/jpg A2592000
    ExpiresByType image/jpeg A2592000
    ExpiresByType text/javascript A2592000
    ExpiresByType application/x-javascript A2592000
    ExpiresByType text/css A2592000

Leave a Reply

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