Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I can't find a way to do it. Is it even possible?

share|improve this question

migrated from stackoverflow.com Aug 25 '09 at 19:11

This question came from our site for professional and enthusiast programmers.

add comment

4 Answers

You could create a custom script and redirect all your images to that script. It's kind of a big overhead but it works if it's your only option.

For example

http://yoururl.com/img.php?image=banner.jpg

And that script would contain for example:

<?php
header('Expires: full_date_comes_here');

echo file_get_contents('images/'.$_GET['image']);
?>

Note!!! This is just a dummy example and you should add validation to your script, to prevent attacks using "../" parts etc.

share|improve this answer
add comment

Yes, mod_expires will work via .htaccess files. Put this in your .htaccess file:

  ExpiresActive on
  ExpiresByType image/gif "access plus 1 day"
  ExpiresByType image/jpeg "access plus 1 day"
  ExpiresByType image/png "access plus 1 day"
share|improve this answer
2  
mod_expires does not work in a shared hosting environment. I just tried this and got a 500 error. –  Jared Jun 12 '10 at 0:28
add comment

Like this:

<FilesMatch "\.(jpg|png|gif|js|css|ico|swf)$">
    Header set Expires "Thu, 31 Dec 2037 23:55:55 GMT"
    Header set Cache-Control "public,max-age=315360000"
    Header set Vary "Accept-Encoding"
</FilesMatch>

If you want a shorter caching period, remove Header set Expires and adjust Header set Cache-Control.

share|improve this answer
add comment

Windows based hosting with godaddy doesnt support .htaccess, you have to use web.config.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.