How to set different HTTP Expire Header for multiple images of same type

HTTP expire header can be used to set a expiry time for an image, css or any other content type. The browser does not fetch that content till it has crossed the expire timestamp. Common images are good candidates for setting expire time to some high value in future.

Sample expire header

Here is how an expire header looks like in HTTP response headers:

Expires: Sat, 27 Apr 2013 21:32:03 GMT

This header will mean that browser can cache this content till the date mentioned in the header. The higher the value, the better it is from performance and page speed perspective. Setting too high value will have downside that customer may see a stale image if the image is modified and image name is kept same.

Setting Expire header for all files a type

To set expire for all files of a specific type (e.g. image/png), here is the configuration you can use:

ExpiresByType image/png "access plus 14 days"

Setting expire header for specific files

Sometimes you may want to keep a high expire time for some selective images which appear on all pages. e.g. icons, etc. Here is an apache configuration to achieve that:

ExpiresActive On
<Files ~ "^(file1\.png|file2\.png)$">
  ExpiresByType image/png "access plus 14 days"
</Files>

This will set Expire Header only for file1.png and file2.png. In case you want to have different Expire header for some other files, you can add additional <Files> directive in Apache conf file.

Share this article: share on Google+ share on facebook share on linkedin tweet this submit to reddit

Comments

Click here to write/view comments