views:

74

answers:

2

I found out that you cannot use a RewriteCond backreference as a pattern condition in the next Rewrite Cond...

What I have looks right, but Tim is correct in the comment bellow... This is still passing when we are beyond the expiration date saved in the cookie.

Any ideas on how to actually test the value instead of the existence?

RewriteCond %{HTTP_COOKIE} niftyExpire=([^;\ ]+)
RewriteRule .*index.html /fake.html [E=niftyExpire:%1,C]
RewriteCond %{TIME} <{ENV:niftyExpire}
RewriteRule .*fake.html /pass.html [R,L]
RewriteRule .*index.html /fail.html [R,L]

Thanks,

-Eric

+1  A: 

Other than the extra .*s in the last two rules, it looks good to me. What do you expect after the extensions?

Aaron Harun
As it turns out, mod_rewrite doesn't expand any variables in the test pattern, so I am effectively only testing for the existence of the cookie... Any ideas on how to actually test the value instead of just the existence?
Eric
+3  A: 

Alright, so it turns out that you can theoretically make this work. However, and let me stress this very clearly, do not, under any circumstances, do this.

Compared to using a script, using a program-backed RewriteMap (although not always possible), or in an extreme scenario compiling a modified mod_rewrite, I can only imagine that the performance of this is absolutely horrible, and since it has to be prepared on every single request when used in a .htaccess file, that would lead to an irresponsibly large amount of overhead.

That being said, for proof of concept, 132 lines of mod_rewrite blasphemy:

RewriteEngine On

RewriteCond %{HTTP_COOKIE} niftyExpire=([^;\ ]+) [OR]
RewriteCond fail ^(fail)$
RewriteRule index.html$ /fail.html [E=niftyExpire:%1,E=RW_TSTATUS:%1]

RewriteCond %{ENV:RW_TSTATUS) !=fail
RewriteCond %{ENV:niftyExpire} ^(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})\d{2}$
RewriteRule .* - [E=RW_CYEAR:%1,E=RW_CMON:%2,E=RW_CDAY:%3,E=RW_CHOUR:%4,E=RW_CMIN:%5]

# Check if current YY*YMMDDHHMM < cookie YY*YMMDDHHMM
#(applicable only for this decade..but that's good enough)
RewriteCond %{TIME_YEAR}%{ENV:RW_CYEAR} !^(\d{4})\1$
RewriteCond %{TIME_YEAR}%{ENV:RW_CYEAR}  ^\d{2}(\d)\d{3}(\d)\d$
RewriteCond %1%2 ^(01|[01]2|[0-2]3|[0-3]4|[0-4]5|[0-5]6|[0-6]7|[0-7]8|[0-8]9)$
RewriteRule .* - [E=RW_TSTATUS:success]

RewriteCond %{ENV:RW_TSTATUS} !fail|success
RewriteCond %{TIME_YEAR}%{ENV:RW_CYEAR}  ^\d{2}(\d)\d{3}(\d)\d$
RewriteCond %1%2 !^(\d)\1$
RewriteRule .* - [E=RW_TSTATUS:fail]

# Check if current YYY*MMDDHHMM < cookie YYY*MMDDHHMM
RewriteCond %{ENV:RW_TSTATUS} !fail|success
RewriteCond %{TIME_YEAR}%{ENV:RW_CYEAR} !^(\d{4})\1$
RewriteCond %{TIME_YEAR}%{ENV:RW_CYEAR}  ^\d{3}(\d)\d{3}(\d)$
RewriteCond %1%2 ^(01|[01]2|[0-2]3|[0-3]4|[0-4]5|[0-5]6|[0-6]7|[0-7]8|[0-8]9)$
RewriteRule .* - [E=RW_TSTATUS:success]

RewriteCond %{ENV:RW_TSTATUS} !fail|success
RewriteCond %{TIME_YEAR}%{ENV:RW_CYEAR}  ^\d{3}(\d)\d{3}(\d)$
RewriteCond %1%2 !^(\d)\1$
RewriteRule .* - [E=RW_TSTATUS:fail]

# Check if current YYYY*MDDHHMM < cookie YYYY*MDDHHMM
RewriteCond %{ENV:RW_TSTATUS} !fail|success
RewriteCond %{TIME_MON}%{ENV:RW_CMON}   !^(\d{2})\1$
RewriteCond %{TIME_MON}%{ENV:RW_CMON}    ^(\d)\d(\d)\d$
RewriteCond %1%2 ^01$
RewriteRule .* - [E=RW_TSTATUS:success]

RewriteCond %{ENV:RW_TSTATUS} !fail|success
RewriteCond %{TIME_MON}%{ENV:RW_CMON}    ^(\d)\d(\d)\d$
RewriteCond %1%2 !^(\d)\1$
RewriteRule .* - [E=RW_TSTATUS:fail]

# Check if current YYYYM*DDHHMM < cookie YYYYM*DDHHMM
RewriteCond %{ENV:RW_TSTATUS} !fail|success
RewriteCond %{TIME_MON}%{ENV:RW_CMON}   !^(\d{2})\1$
RewriteCond %{TIME_MON}%{ENV:RW_CMON}    ^\d(\d)\d(\d)$
RewriteCond %1%2 ^(01|[01]2|[0-2]3|[0-3]4|[0-4]5|[0-5]6|[0-6]7|[0-7]8|[0-8]9)$
RewriteRule .* - [E=RW_TSTATUS:success]

RewriteCond %{ENV:RW_TSTATUS} !fail|success
RewriteCond %{TIME_MON}%{ENV:RW_CMON}    ^\d(\d)\d(\d)$
RewriteCond %1%2 !^(\d)\1$
RewriteRule .* - [E=RW_TSTATUS:fail]

# Check if current YYYYMM*DHHMM < cookie YYYYMM*DHHMM
RewriteCond %{ENV:RW_TSTATUS} !fail|success
RewriteCond %{TIME_DAY}%{ENV:RW_CDAY}   !^(\d{2})\1$
RewriteCond %{TIME_DAY}%{ENV:RW_CDAY}    ^(\d)\d(\d)\d$
RewriteCond %1%2 ^(01|[01]2|[0-2]3)$
RewriteRule .* - [E=RW_TSTATUS:success]

RewriteCond %{ENV:RW_TSTATUS} !fail|success
RewriteCond %{TIME_DAY}%{ENV:RW_CDAY}    ^(\d)\d(\d)\d$
RewriteCond %1%2 !^(\d)\1$
RewriteRule .* - [E=RW_TSTATUS:fail]

# Check if current YYYYMMD*HHMM < cookie YYYYMMD*HHMM
RewriteCond %{ENV:RW_TSTATUS} !fail|success
RewriteCond %{TIME_DAY}%{ENV:RW_CDAY}   !^(\d{2})\1$
RewriteCond %{TIME_DAY}%{ENV:RW_CDAY}    ^\d(\d)\d(\d)$
RewriteCond %1%2 ^(01|[01]2|[0-2]3|[0-3]4|[0-4]5|[0-5]6|[0-6]7|[0-7]8|[0-8]9)$
RewriteRule .* - [E=RW_TSTATUS:success]

RewriteCond %{ENV:RW_TSTATUS} !fail|success
RewriteCond %{TIME_DAY}%{ENV:RW_CDAY}    ^(\d)\d(\d)\d$
RewriteCond %1%2 !^(\d)\1$
RewriteRule .* - [E=RW_TSTATUS:fail]

# Check if current YYYYMMDD*HMM < cookie YYYYMMDD*HMM
RewriteCond %{ENV:RW_TSTATUS} !fail|success
RewriteCond %{TIME_HOUR}%{ENV:RW_CHOUR} !^(\d{2})\1$
RewriteCond %{TIME_HOUR}%{ENV:RW_CHOUR}  ^(\d)\d(\d)\d$
RewriteCond %1%2 ^(01|[01]2)$
RewriteRule .* - [E=RW_TSTATUS:success]

RewriteCond %{ENV:RW_TSTATUS} !fail|success
RewriteCond %{TIME_HOUR}%{ENV:RW_CHOUR}  ^(\d)\d(\d)\d$
RewriteCond %1%2 !^(\d)\1$
RewriteRule .* - [E=RW_TSTATUS:fail]

# Check if current YYYYMMDDH*MM < cookie YYYYMMDDH*MM
RewriteCond %{ENV:RW_TSTATUS} !fail|success
RewriteCond %{TIME_HOUR}%{ENV:RW_CHOUR} !^(\d{2})\1$
RewriteCond %{TIME_HOUR}%{ENV:RW_CHOUR}  ^\d(\d)\d(\d)$
RewriteCond %1%2 ^(01|[01]2|[0-2]3|[0-3]4|[0-4]5|[0-5]6|[0-6]7|[0-7]8|[0-8]9)$
RewriteRule .* - [E=RW_TSTATUS:success]

RewriteCond %{ENV:RW_TSTATUS} !fail|success
RewriteCond %{TIME_HOUR}%{ENV:RW_CHOUR}  ^(\d)\d(\d)\d$
RewriteCond %1%2 !^(\d)\1$
RewriteRule .* - [E=RW_TSTATUS:fail]

# Check if current YYYYMMDDHH*M < cookie YYYYMMDDHH*M
RewriteCond %{ENV:RW_TSTATUS} !fail|success
RewriteCond %{TIME_MIN}%{ENV:RW_CMIN}   !^(\d{2})\1$
RewriteCond %{TIME_MIN}%{ENV:RW_CMIN}    ^(\d)\d(\d)\d$
RewriteCond %1%2 ^(01|[01]2|[0-2]3|[0-3]4|[0-4]5)$
RewriteRule .* - [E=RW_TSTATUS:success]

RewriteCond %{ENV:RW_TSTATUS} !fail|success
RewriteCond %{TIME_MIN}%{ENV:RW_CMIN}    ^(\d)\d(\d)\d$
RewriteCond %1%2 !^(\d)\1$
RewriteRule .* - [E=RW_TSTATUS:fail]

# Check if current YYYYMMDDHHM* < cookie YYYYMMDDHHM*
RewriteCond %{ENV:RW_TSTATUS} !fail|success
RewriteCond %{TIME_MIN}%{ENV:RW_CMIN}   !^(\d{2})\1$
RewriteCond %{TIME_MIN}%{ENV:RW_CMIN}    ^\d(\d)\d(\d)$
RewriteCond %1%2 ^(01|[01]2|[0-2]3|[0-3]4|[0-4]5|[0-5]6|[0-6]7|[0-7]8|[0-8]9)$
RewriteRule .* - [E=RW_TSTATUS:success]

RewriteCond %{ENV:RW_TSTATUS} !fail|success
RewriteCond %{TIME_MIN}%{ENV:RW_CMIN}    ^\d(\d)\d(\d)$
RewriteCond %1%2 !^(\d)\1$
RewriteRule .* - [E=RW_TSTATUS:fail]

RewriteCond %{ENV:RW_TSTATUS} !=fail
RewriteRule .* /pass.html

If anyone has an easier way, or could suggest a way this could be simplified even more, I'm all ears. I've probably committed a crime against humanity by writing this monstrosity, but at least we can say it isn't impossible now.

Tim Stone
Wow! Thanks for that :-)I've found another unrelated solution for my problem (checking a member ID against a directory with a file for each active member)But this is a super-creative solution :-)Best, -Eric
Eric