Mobile Number Tracker
Trace Mobile Number Earn Money Online
© TechWelkin.com

How to Add Expires Header to Images

Samyak Lalit | March 26, 2012 (Last update: November 10, 2014)

Samyak Lalit is an Indian author and disability rights activist. He is the principal author and founder of projects like TechWelkin, WeCapable, Viklangta, Kavita Kosh among many others.

Caching is a very important aspect that every webmaster must be aware of. Websites that do not properly utilize caching will load slowly in web browsers. Website speed is a crucial thing in the eyes of modern search algorithms. Images load slowest because of their bit size. Webpages full of images load terribly slow! Let’s learn how to use expires header to cache images.

Expires header tells browser how to cache a web page component. If a component (image, flash, html etc.) is not going to change for a long time in future –it is highly recommended that you add a far future expires header. In simple words, this means that you tell the browser to cache the component for a long time (several months or may be several years).

If you believe that a component is never going to change –you should add never expires header.

Add Expires Header for Images in Apache Server

In case you are using Apache server, you can use the following in .htaccess file:

<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType text/html "access plus 2 days"
ExpiresByType image/gif "access plus 60 days"
ExpiresByType image/jpg "access plus 60 days"
ExpiresByType image/png "access plus 60 days"
ExpiresByType application/x-javascript "access plus 60 days"
ExpiresByType text/css "access plus 60 days"
ExpiresByType image/x-icon "access plus 360 days"
</IfModule>

There is another way to accomplish the same thing. It is more concise mehtod:

<filesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf)$">
Header set Expires "Thu, 15 Apr 2010 20:00:00 GMT"
</filesMatch>
Add Expires Header for Images in IIS Server

In case of IIS Server 7 or later, add the following code in web.config file’s system.webServer section

<staticContent>
 <clientCache httpExpires="Sun, 29 Mar 2020 00:00:00 GMT" cacheControlMode="UseExpires" />
</staticContent>

This is how you can easily add expires header in Apache and IIS server. If you have control of your server, it is highly recommended to add these headers to enhance your website’s performance.

I hope it was useful. Please feel free to ask me if you have any question about this topic. I will be happy to try and help you. Thank you for using TechWelkin.

© TechWelkin.com

One response to “How to Add Expires Header to Images”

  1. emma williams says:

    Thank you for sharing this blog, it can really help me alot adding the expire tag.

Leave a Reply

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