0

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.

brentonstrine
  • 21,694
  • 25
  • 74
  • 120

1 Answers1

1

You're going to be way better off just writing a script to do this. Date comparison using mod_rewrite, if someone even manages to do it, sounds like a horrible idea. How are you going to code it for previous years? (Like file_2013-12-30.txt and file_2014-01-30.txt)

The functionality of mod_rewrite is completely inadequate to do this kind of comparison.

You'll have to create groupings for the year, month and date, then do comparisons with them all without the use of any conditionals (there's no If/Then/Else in mod_rewrite, you have to enumerate every possible condition every time for every rule) or controlled loops, without any arithmetic functions, and without the means to find a file with the next lowest number for a date, or a date/month, or a date/month/year. Which means for a request like file_2014-01-10.txt you have to check if the file file_2014-01-09.txt exists, then if file_2014-01-08.txt exists, then if file_2014-01-07.txt exists, etc. All without being able to do any math.

Not saying it isn't possible to create some clever way to make it all work somehow, but God help whoever has to make a tweak to it afterwards.

Jon Lin
  • 142,182
  • 29
  • 220
  • 220
  • Just out of curiosity, is there something about dealing with a previous year that is different or more difficult than dealing with a previous day or month? Isn't it all the same, since they sort alphabetically? – brentonstrine Jan 31 '14 at 19:55
  • _"...and without the means to find a file with the next lowest number..."_--This is the key thing I was hoping would be possible. Thanks for the insight. – brentonstrine Jan 31 '14 at 20:08
  • @brentonstrine Yeah, the only check you can use is if a string maps to an existing file (`-f`), a directory (`-d`), a symlink, a non-empty file, etc. No way to get a list of all files and do comparison on them. – Jon Lin Jan 31 '14 at 20:11