How to Add Expires Header to Images

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.

1 thought on “How to Add Expires Header to Images”

Leave a Comment

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