I am finding that my web pages' images are always getting loaded from my server, even though they never change. Since these are images, not pages, I can't include an expiration date for them... How can I tell browsers to avoid reloading images they already have?

In PHP I tried using this:

  header ("ExpiresByType image/gif \"modification plus 10 minutes\"");

but this causes an internal server error.


Your header is incorrect. Here is some documentation. It should be Expires as in:

 header("Expires: Thu, 30 May 2013 11:26:00 GMT");

You generally generate it in PHP like this:

 header('Expires: '.date('D, d M Y H:i:s', time() + 60*10).' GMT');

There is no need to put a type because it is irrelevant. The header simply applies to the result you are serving. You can also use the max-age header instead:

header('Cache-Control: max-age=600');

ExpiresByType is an Apache directive, not PHP. Typically you would put this in a .htaccess file in the same folder as the images (or in the vhost):

ExpiresByType image/gif "modification plus 10 minutes"

You will need to have the Apache module mod_expires enabled for this to work.

  • "access plus 10 minutes" would probably be more appropriate, unless maybe these images really are being updated every 10 minutes? – MrWhite Sep 15 '15 at 11:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.