Redirect based on time of day

Problem:

You wish to serve different content depending on what time of day it is

Recipe:

RewriteEngine on
RewriteCond   %{TIME_HOUR}%{TIME_MIN} >0700
RewriteCond   %{TIME_HOUR}%{TIME_MIN} <1900
RewriteRule   ^/foo\.html$             /foo.day.html [PT]
RewriteRule   ^/foo\.html$             /foo.night.html [PT]

Discussion:

This ruleset will rewrite /foo.html to /foo.day.html between 0700 (7am) and 1900 (7pm), and will rewrite it to /foo.night.html the rest of the time. The RewriteCond causes the following RewriteRule to be skipped if the condition is not met, which is how we end up with an if ... else kind of syntax.

Serve a contest page during a contest

Problem

Serve a contest page during a contest, but don't bug people with it more than once a day.

Recipe

RewriteCond %{TIME} >20050701080000
RewriteCond %{TIME} <20050705170000
RewriteCond %{HTTP_COOKIE} !frontdoor=yes
RewriteRule ^/index\.html$  /contest.html \
        [R,CO=frontdoor:yes:.apacheadmin.com:1440,L]

Discussion

If the date is between the two values (8am on July 1 and 5pm on July 5) and if they haven't already come through the front door, redirect /index.html to /contest.html and set a cookie for a day so that they don't have this happen again until tomorrow.

RewriteTimeOfDay (last edited 2009-09-20 22:13:22 by localhost)