My site (actively developed, single server) uses Apache htaccess to redirect to PHP scripts which generate cached versions of the requested files and then serve the cached versions using readfile(). The cached versions have little additions like meta tags that I need.
The first thing the caching script does is check if the cached file exists and is not older than either the original file or the caching script and if so, just serves it with header() and readfile(). This is reasonably fast of course, and it works easily since FTP sets mtime at upload.
However is there a way to get vanilla htaccess to do just the cache logic for me? Can htaccess redirects "branch" based on what is sort of a series of if/thens?
"FileName" -> ("Cached_FileName" does not exist) -> "CachingScript"
"FileName" -> ("Cached_FileName" is older than "FileName") -> "CachingScript"
"FileName" -> ("Cached_FileName" is older than "CachingScript") -> "CachingScript"
"FileName" -> "Cached_FileName"
It's not that I am experiencing any problems with my setup now -- just that I was wondering if htaccess could do this useful task itself, saving a PHP invocation for most hits, but keeping the ease of using mtime for active development.
My Answer:
to cached files with newer modification dates?
Unfortunately, Apache's htaccess file does not have the capability to check for file modification dates and serve cached files based on that information. You would need to implement this logic within your PHP caching script.
One possible solution is to modify your caching script to check the modification dates of both the original file and the cached file, and serve the cached file only if it is newer than the original file. This way, you can ensure that your cached files are always up to date with the latest changes to the original files.
Alternatively, you could look into using a caching plugin or module for Apache that provides more advanced caching capabilities, such as checking for file modification dates and serving cached files based on that information.
Overall, while htaccess can be used to redirect requests to cached files, it does not have built-in functionality to check for file modification dates and serve cached files based on that information. You will need to implement this logic within your caching script or look into using a more advanced caching solution.
Rate this post
5 of 5 based on 3696 votesComments