How to Add Far Future Expires Headers to Your WordPress Site

Categories: Site Optimization Tips

Few weeks ago I wrote on how to optimize your WordPress site for better performance which has some tips and tricks on how to speed up a WordPress Site. I received a few requests to elaborate on some of the points as not all of them are obvious changes. In this article I have explained how you can add far future expiry header to your image, css and javascript files to speed up your site.

Adding expires headers do not affect the site load time for a first time visitor but you will be surprised how much the page load time decreases (faster page load) for subsequent page views/return visits from that visitor. Expires header specifies a time far enough in the future so that browsers won’t try to re-fetch images, CSS, javascript etc files that haven’t changed (this reduces the number of HTTP requests) and hence the performance improvement on subsequent page views.

If your server is Apache (most web servers), you can use the ‘ExpiresDefault’ directive to set an expiration date relative to the current date.

ExpiresDefault “access plus 2 months”

This sets the expiry date of the file 2 months into the future from the current time. The following values can be used to specify the time period:

  • years
  • months
  • weeks
  • days
  • hours
  • minutes
  • seconds

eg. ExpiresDefault “access plus 14 days”

To add expires header to the image, CSS, javascript files add the following to your .htaccess file
#Expire Header
<FilesMatch "\.(ico|jpg|jpeg|png|gif|js|css|swf)$">
ExpiresDefault "access plus 2 hours"
</FilesMatch>

or
# Expire images header
ExpiresActive On
ExpiresDefault A0
ExpiresByType image/gif A2592000
ExpiresByType image/png A2592000
ExpiresByType image/jpg A2592000
ExpiresByType image/jpeg A2592000
ExpiresByType image/ico A2592000
ExpiresByType text/css A2592000
ExpiresByType text/javascript A2592000

A2592000 means 1 month in the future (60*60*24*30=2592000)

Keep in mind that when you use expires header the files are cached in the browser until it expires so do not use this on files that changes frequently. If you change/update a file that has a far future expiry (eg. CSS or javascript files) then you should rename that file and use the renamed version so the browser doesn’t fetch the old file.

Removing ETags

According to Wikipedia – “An ETag (entity tag) is an HTTP response header returned by an HTTP/1.1 compliant web server used to determine change in content at a given URL. When a new HTTP response contains the same ETag as an older HTTP response, the contents are considered to be the same without further downloading.”

ETags were added to provide a mechanism for validating entities that is more flexible than the last-modified date but If you’re not taking advantage of the flexible validation model that ETags provide, it’s better to just remove the ETag altogether. Removing the ETag reduces the size of the HTTP headers in the response and subsequent requests thus improving site performance.

Add the following to your .htaccess file to remove ETags:

FileETag none

These tweaks can dramatically improve the performance of your site even though they are minor and doesn’t take that long to apply. I have seen a 20% speed improvement on my page loads just by adding these tweaks. My home page used to take around 5 seconds to load but then it dropped to around 3 seconds after these tweaks. Below is a screenshot of YSlow:
Screenshot of my Home Page Load Time

Articles you may also like:

  1. WordPress Optimization Tips and Tricks for Better Performance and Speed
  2. How to Create and Display Favicon to Make Your Site Stand Out in The Pile of Bookmarks
  3. How to Install WordPress on Your Blog (Instructions with Screenshots)
  4. How to control access of the web crawlers or web robots to your site
  5. How to Fix the Character Encoding Problem in WordPress
Tags: , , , , ,

Subscribe to Tips and Tricks HQ to stay informed

twitter_icon

106 Responses.

  • #1 by agame.com on January 20, 2012 - 1:08 pm

    thanks for sharing tips! I really like what you guys are up too. Such clever work and coverage! Keep up the terrific works guys I’ve added you guys to our blogroll.

  • #2 by poptropica on November 19, 2011 - 1:45 am

    The code works like a charm, I really appreciate for sharing your Big idea to us. Very Helpful. Thank a lot.

  • #3 by starfall on November 17, 2011 - 7:07 pm

    You are amazingly great. I appreciate the tutorial, very helpful. nice one admin, page load on website gains high performance and seo friendly to search engines.

  • #4 by Timber Flooring on November 14, 2011 - 10:25 am

    Thanks for the information, has made my site faster, and like you say doesn`t take much time to do !!!

    Thanks again

  • #5 by Roger Sanchez on November 13, 2011 - 2:55 am

    Hi guys (and gals)…

    Once again, very exceptional: above it, actually…Helped me gain an edge in my cache (plus other methods I’ve started for my site: DNS, sprites, etc.)…I’ve seen load-times (initial & cached) jump to amazing levels…This was just one of the ideas that helped me attain those high-marks on this project…

    Thanks again for sharing & have a great day!

    Roger

  • #6 by starfall on November 11, 2011 - 7:36 am

    totally useful, and if anyone whose not much of a coder type, you may use plugins. try searching for seo plugins or minifying the theme itself.

  • #7 by Elin on November 7, 2011 - 1:44 am

    Thank you. Applied on my blog, my blog so much faster now

    For wordpress user, http://wordpress.org/extend/plugins/w3-total-cache/ is best way to apply this script. Thank you

  • #8 by Mike on October 16, 2011 - 5:45 pm

    searching for “setting expiry date for jpg” in google brought me to your site and in just minutes i got 3 % more page speed and an upgrade to A in Yslow! my site Americaurlaub.de means americavacancies and has some infos for tourists that are interested in visiting America. native german speakers, mostly from germany, austria and switzerland.
    thanks to you, the site is running more faster now.
    Mike Hutner

  • #9 by seobalu on October 15, 2011 - 7:42 am

    Thank you for sharing this info. I was able to add expires header by modifying the .htaccess file and now my site is loading faster… amazing!

  • #10 by Aluvsa on October 10, 2011 - 5:58 am

    Thank you for the article, I have copied and paste to my blog .htaccess :)

    Regards

  • #11 by Michael | BuildinganOnline.com on September 25, 2011 - 4:06 am

    Thank you for a great explanation of thees too issues. It made me understand what’s going on much better.

    I also discovered that there is a fantastic plugin actually doing the job for you.

    Now, I’m not a big fan of plugin’s – to many only slow down your site – but since this particular one does so much more good for your seo, than just this, I believe it’s worth the(eventual) loss of speed to install it anyway.
    http://wordpress.org/extend/plugins/w3-total-cache/

    I also found some additional great help on speeding up your site at Yahoo.com
    http://developer.yahoo.com/performance/rules.html#num_http

    Ones again, thank you for your article. I’ll sure come back and see what else you have to tell.

    :)
    Michael

  • #12 by Y8 on September 8, 2011 - 5:02 pm

    i heard this expires on today and i search all the way to internet and I found your blog. I’m happy really just install in to my site.

  • #13 by Chileshe on September 5, 2011 - 1:41 am

    very thoughtful peace of idea. I will try it otherwise will be great if you can help and just pay you for the service!

  • #14 by Stardoll on September 4, 2011 - 2:20 am

    hey admin, the expires option is such a wonderful code …this cache the expires date of my files and so far so good.. It 4.3/5 rating now.. Thanks..

  • #15 by Miniclip on September 4, 2011 - 2:18 am

    I never tried hope it will make fast… I implement it right now..

  • #16 by friv on August 31, 2011 - 12:36 am

    thanks for share, It has information I have been searching for a long time

  • #17 by riza on August 17, 2011 - 3:44 pm

    Thank you very much, work fine on my blog, page speed 92% and yslow 89%. :)

  • #18 by FeedTheFlow.com on August 16, 2011 - 2:02 pm

    I might be new to this but thanks alot. That helped me improve my page load speed.

  • #19 by Flooring on August 9, 2011 - 4:34 am

    Thanks for your well written article, I`m starting to understand this now, thanks matey!!

  • #20 by Rasika on July 31, 2011 - 1:54 pm

    I was searching for how to add “expires in the headers” .so found your great article Thank you very much..

  • #21 by friv on July 26, 2011 - 5:11 am

    very good article, very useful for my site. Thx!

  • #22 by Friv on July 25, 2011 - 10:20 am

    Nice article. Thank you a lot for share!

  • #23 by Friv on July 20, 2011 - 3:03 am

    Thank you for sharing. Great post!

  • #24 by jeux on July 2, 2011 - 7:33 pm

    very good article I need it Thanks for the post.

  • #25 by Lifyerori on June 26, 2011 - 5:27 pm

    Nice site ! I will save it in my favorites. thanks

  • #26 by Friv on June 7, 2011 - 4:11 am

    very good article I need it Thanks for the post.

  • #27 by Alang Fauzi Bertuah on May 30, 2011 - 3:18 pm

    Thanks for sharing :)

    I’ve tried it n this trick work very well. My blog run faster than before.

  • #28 by Friv on May 23, 2011 - 2:58 am

    Thank you for sharing, I like it very much

  • #29 by Susan on May 22, 2011 - 1:14 am

    This is quite helpful. I want to do the most I can to make things faster for my sites.

  • #30 by Imran@shariffDotOrg on May 18, 2011 - 1:12 am

    When I page the above code in .htaccess I am getting internal Error, however I have managed to get my scores to 70% which is C Grade. Still I have some issues left like use a CDN, Add Expires Headers, Compress Componenets with gzip and Reduce DNS lookups.

    I am still figuring out how to solve this problems.. I am using cloud hosting from nethosting. anything to be done in WHM.

    Required your suggestion.

    Thanks
    -Imran

  • #31 by admin on April 29, 2011 - 9:03 pm

    @Friv, you will most likely need to change the time to more than 1 year for the tool to recognize it but you can be sure that your images are being cached by browsers for that 2 month (when you browse various pages of your site you will notice the difference)

  • #32 by friv on April 29, 2011 - 3:02 am

    Ive placed the first code in my htaccess file and changed it to 2 months, but Yslow still says I have nothing? Any suggestions. If you need any more info just say.

    Thanks

  • #33 by Friv on April 28, 2011 - 2:42 am

    Thanks!
    I am finding it.

  • #34 by admin on April 11, 2011 - 9:45 pm

    @Nathan, You can load one of your site’s page with images few times and get an average page load time. Then apply this technique and record a few more page load time after. Comparing these two sets of data should give you an indication.

  • #35 by Nathan on April 11, 2011 - 6:25 am

    Its not showing up under any testing system. How can i be sure that I have everything right?

    Thanks

  • #36 by Hafiz on April 10, 2011 - 12:38 pm

    Thanks for the tips, very useful. I was trying to find what was A2592000 means. Now I get B on YSlow.

  • #37 by joomla on April 9, 2011 - 3:58 pm

    very useful for my site. Thx!

  • #38 by Victor chileshe luo on April 8, 2011 - 6:19 am

    lets put this to the test! thank you in advance.

  • #39 by Nathan on April 6, 2011 - 6:34 am

    really, a whole year! OK. many thanks for the help, appreciate it.

  • #40 by admin on April 5, 2011 - 10:47 pm

    @Nathan, You will need to set the time to 1 year or more to get the score from Yslow but it doesnt’ mean its not working. Your static content will still be cached for 2 months and you will see an increase in performance.

  • #41 by friv on April 5, 2011 - 9:56 pm

    Thanks for giving me the useful information. I think I need it. Thank you

  • #42 by nathan on April 5, 2011 - 7:29 am

    Ive placed the first code in my htaccess file and changed it to 2 months, but Yslow still says I have nothing? Any suggestions. If you need any more info just say.

    Thanks

  • #43 by admin on March 11, 2011 - 1:34 am

    Some hosting providers do not allow the “expires header” for their shared hosting accounts. This is probably why its giving a 500 error when you try to use them.

  • #44 by Scott Lincoln on March 10, 2011 - 3:19 pm

    I was wondering will this method of future expires adversely affect my project wonderful ads?

    Also, I am using Godaddy hosting on my WordPress/Comicpress based site. So I accessed my htaccess file from my GoDaddy file manager, I checked the htaccess file and clicked edit, but every time I saved the file after adding either script version I immediately got a 500 error message on my site. When I deleted the new scripting and saved it, the error disappeared and the site functioned normally. The only other scripting in the htaccess file is for Super Cache and a couple other plugins… Is there a certain place to paste the script or certain tags etc?

    I’d really like to be able to have jpgs and pngs cache since I have quite a few images on my site. Am I missing something?

  • #45 by Elizabeth Ivy - www.vietnamvisacheap.com on March 3, 2011 - 10:28 am

    It has been a good resource of information for me.

    Thanks for sharing this.

  • #46 by John Gamings on January 3, 2011 - 11:02 am

    Nice article! This will be quite helpful for me

  • #47 by Kipram on December 21, 2010 - 11:09 am

    Thank you for the code, I have copied and paste to my .htaccess

  • #48 by seo online on December 2, 2010 - 4:05 am

    i am managing my website,realy need this kind of information,really very good information for me,thx very much for sharing with us!
    .-= seo online´s last blog ..Compresses your wordpress =-.

  • #49 by admin on November 16, 2010 - 11:35 pm

    YSlow sometimes does that unless you set it to like a year. You should notice a different in load time even though YSlow is not giving you the score for that particular item.

Featured & Popular Articles

Tips and Tricks Hot Items

wordpress estore plugin
wordpress membership plugin
WordPress PDF Stamper Plugin
WordPress Lightbox Ultimate Plugin
WordPress Affiliate Link Manager Plugin
wordpress affiliate plugin