Say there are four files on a server which contain dates in the filename in some way:
file_2014-01-20.txt
file_2014-01-03.txt
file_2014-01-02.txt
file_2014-01-01.txt
...and the server receives a request for a dated file which doesn't exist:
file_2014-01-10.txt
...is there a way to use mod_rewrite
or some other .htaccess
code to make it find the most recent file that is older than the one requested? E.g. in this case it would return file_2014-01-03.txt
, since that is the most recent existing file that is older than the (nonexistent) requested file.
I know I could redirect all 404's to a special script like this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+) getarchive.php?request=$1
...where getarchive.php
will take request
and use it find and return the correct file. However, this is not a good solution for me for several reasons, and I'd like to do the redirecting entirely with .htaccess
without using any server-side scripts at all if possible.
Edit:
To put the question another way, given a nonexistent filename, can Apache be used to find the next file in the list, when sorted reverse-alphabetically.
Example:
a.txt
b.txt
z.txt
Given a request for n.txt
, the server returns b.txt
.