Home > database >  Apache RewriteCond compare TIME
Apache RewriteCond compare TIME

Time:10-19

I'm struggling with some Apache HTTP server-config.. Specifically I load a date in format YYMMDDHHmmss from a lookup-file via RewriteMap, which I try to compare to the current value from the TIME variable.

  RewriteMap lookup "txt:/path/to/file.flat"
  RewriteCond ${lookup:somekey|NOT_FOUND} NOT_FOUND [or]
  RewriteCond ${lookup:somekey|19700101010101} <%{TIME}

In file.flat would be something like this:

  somekey 20211010042042

It does not seem to work so far, which lead me here. Does anybody have a suggestion on how to tackle this problem?

The last straw I could imagine would otherwise be any kind of script I evaluate the expression with.

CodePudding user response:

RewriteCond ${lookup:somekey|NOT_FOUND} NOT_FOUND [or]
RewriteCond ${lookup:somekey|19700101010101} <%{TIME}

You've not included the complete rule (ie. RewriteRule directive) in your example. Depending on exactly what you are trying to do, you may decide to resolve this a different way.

The main problem with the above condition is that server variables of the form %{VARIABLE} are not expanded in the CondPattern (2nd argument to the RewriteCond directive). So %{TIME} is seen as literal text.

Aside: You don't need two conditions here. If somekey is not found in the rewrite map then both conditions would be true (by the perceived logic of these conditions).

If you are checking for equality then you can use a regex with an internal backreference (for example). However, you are checking "less than" (lexicographic string comparison).

One way to resolve this is to assign the value of the rewrite map lookup to an environment variable and use this in an Apache expression (RewriteCond expr directive - available on Apache 2.4) to make the comparison. You can't lookup the rewrite map directly in the Apache expression.

For example:

RewriteRule ^ - [E=LOOKUP_VALUE:${lookup:somekey}]
RewriteCond expr "reqenv('LOOKUP_VALUE') < %{TIME}"
RewriteRule ^ - [E=SUCCESS:1]

In your example you don't need to explicitly set a DefaultValue (should the key not be found in the rewrite map) since an empty string is always going to be "less than" %{TIME}. (The same as 19700101010101.)

  • Related