FREE THOUGHT · FREE SOFTWARE · FREE WORLD

Get Yesterdays Time in Htaccess

htaccess rewriterules to get yesterdays timeSay you want to Serve Alternate Content based on Time, but instead of serving based off the current time, you want to serve based off yesterdays time, and using .htaccess mod_rewrite rules?

WHY Htaccess!?

A user asked me to help chime in on this issues over at stackoverflow, and this is what I came up with.

Note: This is incredibly inefficient from many different levels. For one thing, any cgi-interpreter such as php, ruby, perl, bash, ocaml, etc., is able to access these variables, and in that context it would only take a single preg_replace or even a couple str_replace's to accomplish the same thing with (arguably) less cpu cycles. But where is the fun in that?

Example of using Mod_Rewrite

Really this was just a quick and easy challenge that I couldn't pass up attempting. It's a great example that illustrates how to at least try to make rewriterules more efficient through the heavy use of the S - SKIP flag. It also is a great illustration of how .htaccess is processed. It's very different from the hugely abstracted and incredibly easy languages most of us use to program today. This harkens back to the days of punch cards, where instructions were really limited and dynamic programming was mostly a dream or a clever hack.

So this code should probably never actually be used in a production environment, but it's a clear and fun example of how mod_rewrite can access and set environment variables that are then accessible by every cgi-interpreter. The goal with mod_rewrite rules, is to write them in such a way that only the bare minimum of rules are actually evaluated and executed. This shows a simplistic example of tightening even highly dramatic/inefficient rules down so that only a couple of them are actually ever processed.

Htaccess RewriteRule to set TIME_YESTERDAY

The following .htaccess rules will set a $_SERVER['TIME_YESTERDAY'] variable to be the TIME of the request minus 24 hours.

# skip next 12 rewriterules if time_day is not 01
RewriteCond %{TIME_DAY} !^01$
RewriteRule .* - [S=12]

RewriteCond %{TIME_MON}%{TIME_DAY} ^0101$
RewriteRule .* - [E=TIME_YESTERDAY:%{TIME_YEAR}1231%{TIME_HOUR}%{TIME_MIN}%{TIME_SEC},S=11]

RewriteCond %{TIME_MON}%{TIME_DAY} ^0201$
RewriteRule .* - [E=TIME_YESTERDAY:%{TIME_YEAR}0131%{TIME_HOUR}%{TIME_MIN}%{TIME_SEC},S=10]

RewriteCond %{TIME_MON}%{TIME_DAY} ^0301$
RewriteRule .* - [E=TIME_YESTERDAY:%{TIME_YEAR}0228%{TIME_HOUR}%{TIME_MIN}%{TIME_SEC},S=9]

RewriteCond %{TIME_MON}%{TIME_DAY} ^0401$
RewriteRule .* - [E=TIME_YESTERDAY:%{TIME_YEAR}0331%{TIME_HOUR}%{TIME_MIN}%{TIME_SEC},S=8]

RewriteCond %{TIME_MON}%{TIME_DAY} ^0501$
RewriteRule .* - [E=TIME_YESTERDAY:%{TIME_YEAR}0430%{TIME_HOUR}%{TIME_MIN}%{TIME_SEC},S=7]

RewriteCond %{TIME_MON}%{TIME_DAY} ^0601$
RewriteRule .* - [E=TIME_YESTERDAY:%{TIME_YEAR}0531%{TIME_HOUR}%{TIME_MIN}%{TIME_SEC},S=6]

RewriteCond %{TIME_MON}%{TIME_DAY} ^0701$
RewriteRule .* - [E=TIME_YESTERDAY:%{TIME_YEAR}0630%{TIME_HOUR}%{TIME_MIN}%{TIME_SEC},S=5]

RewriteCond %{TIME_MON}%{TIME_DAY} ^0801$
RewriteRule .* - [E=TIME_YESTERDAY:%{TIME_YEAR}0731%{TIME_HOUR}%{TIME_MIN}%{TIME_SEC},S=4]

RewriteCond %{TIME_MON}%{TIME_DAY} ^0901$
RewriteRule .* - [E=TIME_YESTERDAY:%{TIME_YEAR}0831%{TIME_HOUR}%{TIME_MIN}%{TIME_SEC},S=3]

RewriteCond %{TIME_MON}%{TIME_DAY} ^1001$
RewriteRule .* - [E=TIME_YESTERDAY:%{TIME_YEAR}0930%{TIME_HOUR}%{TIME_MIN}%{TIME_SEC},S=2]

RewriteCond %{TIME_MON}%{TIME_DAY} ^1101$
RewriteRule .* - [E=TIME_YESTERDAY:%{TIME_YEAR}1031%{TIME_HOUR}%{TIME_MIN}%{TIME_SEC},S=1]

RewriteCond %{TIME_MON}%{TIME_DAY} ^1201$
RewriteRule .* - [E=TIME_YESTERDAY:%{TIME_YEAR}1130%{TIME_HOUR}%{TIME_MIN}%{TIME_SEC}]

# skip next 30 rewriterules if time_day is 01
RewriteCond %{TIME_DAY} ^01$
RewriteRule .* - [S=30]

RewriteCond %{TIME_DAY} ^02$
RewriteRule .* - [E=TIME_YESTERDAY:%{TIME_YEAR}%{TIME_MON}01%{TIME_HOUR}%{TIME_MIN}%{TIME_SEC},S=29]

RewriteCond %{TIME_DAY} ^03$
RewriteRule .* - [E=TIME_YESTERDAY:%{TIME_YEAR}%{TIME_MON}02%{TIME_HOUR}%{TIME_MIN}%{TIME_SEC},S=28]

RewriteCond %{TIME_DAY} ^04$
RewriteRule .* - [E=TIME_YESTERDAY:%{TIME_YEAR}%{TIME_MON}03%{TIME_HOUR}%{TIME_MIN}%{TIME_SEC},S=27]

RewriteCond %{TIME_DAY} ^05$
RewriteRule .* - [E=TIME_YESTERDAY:%{TIME_YEAR}%{TIME_MON}04%{TIME_HOUR}%{TIME_MIN}%{TIME_SEC},S=26]

RewriteCond %{TIME_DAY} ^06$
RewriteRule .* - [E=TIME_YESTERDAY:%{TIME_YEAR}%{TIME_MON}05%{TIME_HOUR}%{TIME_MIN}%{TIME_SEC},S=25]

RewriteCond %{TIME_DAY} ^07$
RewriteRule .* - [E=TIME_YESTERDAY:%{TIME_YEAR}%{TIME_MON}06%{TIME_HOUR}%{TIME_MIN}%{TIME_SEC},S=24]

RewriteCond %{TIME_DAY} ^08$
RewriteRule .* - [E=TIME_YESTERDAY:%{TIME_YEAR}%{TIME_MON}07%{TIME_HOUR}%{TIME_MIN}%{TIME_SEC},S=23]

RewriteCond %{TIME_DAY} ^09$
RewriteRule .* - [E=TIME_YESTERDAY:%{TIME_YEAR}%{TIME_MON}08%{TIME_HOUR}%{TIME_MIN}%{TIME_SEC},S=22]

RewriteCond %{TIME_DAY} ^10$
RewriteRule .* - [E=TIME_YESTERDAY:%{TIME_YEAR}%{TIME_MON}09%{TIME_HOUR}%{TIME_MIN}%{TIME_SEC},S=21]

RewriteCond %{TIME_DAY} ^11$
RewriteRule .* - [E=TIME_YESTERDAY:%{TIME_YEAR}%{TIME_MON}10%{TIME_HOUR}%{TIME_MIN}%{TIME_SEC},S=20]

RewriteCond %{TIME_DAY} ^12$
RewriteRule .* - [E=TIME_YESTERDAY:%{TIME_YEAR}%{TIME_MON}11%{TIME_HOUR}%{TIME_MIN}%{TIME_SEC},S=19]

RewriteCond %{TIME_DAY} ^13$
RewriteRule .* - [E=TIME_YESTERDAY:%{TIME_YEAR}%{TIME_MON}12%{TIME_HOUR}%{TIME_MIN}%{TIME_SEC},S=18]

RewriteCond %{TIME_DAY} ^14$
RewriteRule .* - [E=TIME_YESTERDAY:%{TIME_YEAR}%{TIME_MON}13%{TIME_HOUR}%{TIME_MIN}%{TIME_SEC},S=17]

RewriteCond %{TIME_DAY} ^15$
RewriteRule .* - [E=TIME_YESTERDAY:%{TIME_YEAR}%{TIME_MON}14%{TIME_HOUR}%{TIME_MIN}%{TIME_SEC},S=16]

RewriteCond %{TIME_DAY} ^16$
RewriteRule .* - [E=TIME_YESTERDAY:%{TIME_YEAR}%{TIME_MON}15%{TIME_HOUR}%{TIME_MIN}%{TIME_SEC},S=15]

RewriteCond %{TIME_DAY} ^17$
RewriteRule .* - [E=TIME_YESTERDAY:%{TIME_YEAR}%{TIME_MON}16%{TIME_HOUR}%{TIME_MIN}%{TIME_SEC},S=14]

RewriteCond %{TIME_DAY} ^18$
RewriteRule .* - [E=TIME_YESTERDAY:%{TIME_YEAR}%{TIME_MON}17%{TIME_HOUR}%{TIME_MIN}%{TIME_SEC},S=13]

RewriteCond %{TIME_DAY} ^19$
RewriteRule .* - [E=TIME_YESTERDAY:%{TIME_YEAR}%{TIME_MON}18%{TIME_HOUR}%{TIME_MIN}%{TIME_SEC},S=12]

RewriteCond %{TIME_DAY} ^20$
RewriteRule .* - [E=TIME_YESTERDAY:%{TIME_YEAR}%{TIME_MON}19%{TIME_HOUR}%{TIME_MIN}%{TIME_SEC},S=11]

RewriteCond %{TIME_DAY} ^21$
RewriteRule .* - [E=TIME_YESTERDAY:%{TIME_YEAR}%{TIME_MON}20%{TIME_HOUR}%{TIME_MIN}%{TIME_SEC},S=10]

RewriteCond %{TIME_DAY} ^22$
RewriteRule .* - [E=TIME_YESTERDAY:%{TIME_YEAR}%{TIME_MON}21%{TIME_HOUR}%{TIME_MIN}%{TIME_SEC},S=9]

RewriteCond %{TIME_DAY} ^23$
RewriteRule .* - [E=TIME_YESTERDAY:%{TIME_YEAR}%{TIME_MON}22%{TIME_HOUR}%{TIME_MIN}%{TIME_SEC},S=8]

RewriteCond %{TIME_DAY} ^24$
RewriteRule .* - [E=TIME_YESTERDAY:%{TIME_YEAR}%{TIME_MON}23%{TIME_HOUR}%{TIME_MIN}%{TIME_SEC},S=7]

RewriteCond %{TIME_DAY} ^25$
RewriteRule .* - [E=TIME_YESTERDAY:%{TIME_YEAR}%{TIME_MON}24%{TIME_HOUR}%{TIME_MIN}%{TIME_SEC},S=6]

RewriteCond %{TIME_DAY} ^26$
RewriteRule .* - [E=TIME_YESTERDAY:%{TIME_YEAR}%{TIME_MON}25%{TIME_HOUR}%{TIME_MIN}%{TIME_SEC},S=5]

RewriteCond %{TIME_DAY} ^27$
RewriteRule .* - [E=TIME_YESTERDAY:%{TIME_YEAR}%{TIME_MON}26%{TIME_HOUR}%{TIME_MIN}%{TIME_SEC},S=4]

RewriteCond %{TIME_DAY} ^28$
RewriteRule .* - [E=TIME_YESTERDAY:%{TIME_YEAR}%{TIME_MON}27%{TIME_HOUR}%{TIME_MIN}%{TIME_SEC},S=3]

RewriteCond %{TIME_DAY} ^29$
RewriteRule .* - [E=TIME_YESTERDAY:%{TIME_YEAR}%{TIME_MON}28%{TIME_HOUR}%{TIME_MIN}%{TIME_SEC},S=2]

RewriteCond %{TIME_DAY} ^30$
RewriteRule .* - [E=TIME_YESTERDAY:%{TIME_YEAR}%{TIME_MON}29%{TIME_HOUR}%{TIME_MIN}%{TIME_SEC},S=1]

RewriteCond %{TIME_DAY} ^31$
RewriteRule .* - [E=TIME_YESTERDAY:%{TIME_YEAR}%{TIME_MON}30%{TIME_HOUR}%{TIME_MIN}%{TIME_SEC}]

Thanks to RafaSashi for contributing to this code.

Htaccess Htaccess mod_rewrite RewriteCond RewriteRule TIME TIME_DAY TIME_MIN TIME_MON TIME_SEC

 

 

Comments