How to Add Far Future Expires Headers to Your WordPress Site

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.

You can now use our WP Far Future Expiration Plugin to do this task easily

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.

Video Tutorial (Adding Far Future Expiry Header)

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

Found this resource interesting? Subscribe to Tips and Tricks HQ

email icon rss feed icon twitter icon google plus icon

Comments (122 responses)

  1. admin says:

    @Derek, There shouldn’t be any issue with using this along side W3 Total Cache plugin. It is perfectly okay if you add the htaccess code to the file manually.

  2. Derek says:

    Great post. It has been helpful to track down an issue I was experiencing with a site. Perhaps you can also elaborate on an issue.

    After recently making the switch over from super cache to w3 total cache, I noticed that for all its improvement in site performance, the one line you posted:

    ExpiresDefault “access plus 2 months”

    is not even listed within the htaccess code that W3 includes. Has this been an issue that was missed by them? Or something that nobody thought might make a difference?

    Also, as you have created a plugin to do this simple fix, has there been or will there be any issues using along side of the W3TC plugin?

    As it is not listed in my present htaccess file, I was hoping to add it manually as I could not find it located within W3TC anywhere.

  3. admin says:

    @Hatchemoto, When you say the “WP Plugin”, do you mean the following plugin?
    http://www.tipsandtricks-hq.com/how-to-add-far-future-expires-headers-to-your-wordpress-site-1533

  4. Hatchemoto says:

    The problem people are having with the WP plugin is that it’s not adding the ExpiresActive On directive. Do that, and you will see the results you’re looking for.

  5. Hailey says:

    Hi there, I want to give a huge thumbs up for this great little utility post.

  6. admin says:

    @Lucas, I have seen in the past where YSlow fails to detect the far future expiry correctly on some files. If you have added the rule correctly then the browser will cache them. I think you can ignore the YSlow warning on those files.

  7. Lucas says:

    @admin: thanks for your tip! Unfortunately the issue still persists, I’ve even installed your Far-Futre Expiry plugin, it wouldn’t solve the problem. Any ideas?

  8. admin says:

    @Lucas, you should be able to apply expiry header to the icon file and the JS file by targeting the .ico and .js file extensions. Use a long expiry time (more than 1 year in the future maybe).

  9. Lucas says:

    Hi there!

    Great post, it really helped me out a lot. I added the far-future expiration and it instantly boosted my Y-slow scores :) However, there are a few things remain unsolved – a favicon (png) (no expires), Google fonts api (expires 2013/7/30) and a js file (expires 2013/7/30). These are the info shown in Firebug’s YSlow. What do you suggest me to do?

    Thanks!
    Lucas

  10. admin says:

    @Jeremy, Adding expire headers won’t cause any issue with WP eStore and WP Affiliate Platform plugins.

  11. Jeremy Myers says:

    Do expires headers cause problems with WP eStore and wp Affiliate?

  12. Ron says:

    The commenting system stripped out the code from my examples.

    Basically, just paste this line:
    ExpiresActive ON

    before you specify the expiration timeframes if you’re using the FilesMatch method as mentioned in the post on this page.

  13. yep100 says:

    Thanks for sharing :)

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

  14. admin says:

    @Mukesh, Try a 1 year value rather than 1 month. YSlow sometimes doesn’t catch it if that value is not long enough.

  15. Mukesh Mali says:

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

  16. wpshouter says:

    tnx buddy for this after adding it my YSlow grade jumps to 92 which is pretty impressive :)

  17. ModeShops says:

    Hello,
    I´m using Joomla, but I think it is the same as for WP.

    Thany for the Tipps

  18. Arsie says:

    Thank you for this tip I will try this with my site and see what happens.

  19. kizi says:

    Thanks for giving me the useful information. Thanks!

  20. Elle says:

    Thank you, thank you! Clear concise instructions. I’m just setting up a new site and after testing my page speed saw I should change my set expires. I remember this coming up on an older site a few years ago and not being able to find HOW to change this, when YSlow was telling me I SHOULD change it.

    I was getting a grade B, 81% so lets see what kind of improvement I get after implementing this.

  21. Dave says:

    Thanks for the tip, needed a way to cache images as there are a lot on my site.

  22. Friv says:

    Interesting posts and awesome blog!I will be happy to visit it again!

  23. Computer Repair Redding says:

    Great information…I need the expires header detail to complete my 95% ySlow score. I can’t get to perfect because I don’t use CDN.

    Thanks, Mark

  24. agame.com says:

    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.

  25. poptropica says:

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

  26. starfall says:

    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.

  27. Timber Flooring says:

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

    Thanks again

  28. Roger Sanchez says:

    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

  29. starfall says:

    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.

  30. Elin says:

    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

  31. Mike says:

    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

  32. seobalu says:

    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!

  33. Aluvsa says:

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

    Regards

  34. Michael | BuildinganOnline.com says:

    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

  35. Y8 says:

    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.

  36. Chileshe says:

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

  37. Stardoll says:

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

  38. Miniclip says:

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

  39. friv says:

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

  40. riza says:

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

  41. FeedTheFlow.com says:

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

  42. Flooring says:

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

  43. Rasika says:

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

  44. friv says:

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

  45. Friv says:

    Nice article. Thank you a lot for share!

  46. Lifyerori says:

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

  47. Alang Fauzi Bertuah says:

    Thanks for sharing :)

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

  48. Susan says:

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

  49. Imran@shariffDotOrg says:

    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

  50. admin says:

    @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)

Speak Your Mind

*