Take the 2-minute tour ×

I have a redirect setup that looks for an image in a directory and if it does not exist, it will redirect to a php file that resizes an image and saves it to that directory. I got it to work great. Problem now is that I need to find a good way to redirect it if the image that was written is 4 hours old. I looked into Mod_expires but could not find a way to redirect to my php image resizer when the image is 4 hours old.

I know I could do this in PHP but the whole point was to reduce the server load by using apache instead of PHP. The site I'm working on gets several thousand viewers daily so I really need to efficiently cache the resized images. Here's an example of what I'm doing...

  1. Browser requests "http://images.mydomain.com/image/1234_01_thumb.jpg"
  2. Apache Checks if "http://images.mydomain.com/image/1234_01_thumb.jpg" exists
  3. If it does not exist, redirect to to "/image/resize_image.php" and generate a thumbnail in "/images/" folder

Now that I got that working how do I get it to redirect to "/image/resize_image.php" when the "1234_01_thumb.jpg" gets x hours old? At some point someone is going to have to update that photo especially if they uploaded the wrong one so it can't be permanent.

Please help. Thanks in advance!

share|improve this question
add comment

2 Answers


You can get the file date and then check that against the current time()

say something like this:

$img_head = exif_read_data($img_path, 'IFD0');
if ($img_head != false) {
  if ($img_head['FILE.FileDateTime'] < (time() - 14400)) {
    header ("Location: old_image.php");

I really hope you get what I'm trying to say here as I'm typing from my phone...

share|improve this answer
Thanks Chris, I've already considered this solution before. The problem is now that I have an image in the /image/ folder that PHP has generated, I need apache to redirect back to the image_resize.php if the requested image is old. –  He-Man Mar 31 '11 at 5:41
add comment

Well, I hope someone corrects me if I'm wrong, but the last I knew PHP is run as a module in Apache, via mod_php, so one way or another the web server is still having to churn through your code.

Hmm. My original thought was to set the cache-control value using something like:

<FilesMatch "\.(jpg|jpeg|png|gif)$">
Header set Cache-Control "max-age=14400, must-revalidate"

but that won't work. That would just tell Apache to grab a new copy of the image...


Yeah, @chris_mcclellan's idea is where I was headed in my thought process. I'd run with that and see if it meets your performance goals.

share|improve this answer
add comment

Your Answer


