He-Man
He-Man

Reputation: 11

How to redirect or mod_rewrite an Expired image in a directory using .htaccess

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!

Upvotes: 1

Views: 432

Answers (2)

Tieson T.
Tieson T.

Reputation: 21246

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"
</FilesMatch>

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

EDIT:

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.

Upvotes: 0

Chris McClellan
Chris McClellan

Reputation: 1105

http://php.net/manual/en/function.exif-read-data.php

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");
    exit;
  }
}

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

Upvotes: 1

Related Questions