FREE THOUGHT · FREE SOFTWARE · FREE WORLD

Home » Htaccess » Serve Alternate Content based on Time

by 13 comments

Time-dependant rewriting uses mod_rewrite and apache server time variables to display different files depending on the time, while the URL stays the same. An often requested implementation of this is to display a different home page or image depending on if its morning, noon, or night.


Its also great for locking down access to a file or directory so that it can only be viewed on a certain day, whether that day is a Tuesday or July 10, 2007 is up to you! Another cool use is being able to implement a sort of load-balancing system.

For instance, say you have an mp3 hosted at http://site1.com/1.mp3 but you need to cut the number of requests on that server in half to meet bandwidth restrictions you can use Time Dependent rewriting to force every download request made in the last 30 seconds of every minute to download from an alternate site like http://mirror1.com/1.mp3. You could in fact serve the same file from 60 different servers depending on what second (TIME_SEC) the request was made. Apache mod_rewrite is sweet!

Time and Date RewriteCond Variables

TIME_YEAR
current four-digit year
TIME_MON
current month (0-11)
TIME_DAY
current date
TIME_HOUR
current hour (0-23)
TIME_MIN
current minute (0-59)
TIME_SEC
current second-count (0-59)
TIME_WDAY
current week-day (0-6)
TIME
a formatted string representing the time ex. 20070710212132

mod_rewrite code to output current TIME variables

Use this mod_rewrite code in your .htaccess file to find out what your servers current variables are. Simply uncomment whichever line (one at a time) you want to see the value for, and request any file from your site. So http://site.com/index.html would redirect you to http://site.com/index.html?time=2007 if you had uncommented the TIME_YEAR line.

RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} !time [NC]
#RewriteCond %{TIME} ^(.*)
#RewriteCond %{TIME_YEAR} ^(.*)
#RewriteCond %{TIME_MON} ^(.*)
#RewriteCond %{TIME_WDAY} ^(.*)
#RewriteCond %{TIME_DAY} ^(.*)
#RewriteCond %{TIME_HOUR} ^(.*)
#RewriteCond %{TIME_MIN} ^(.*)
#RewriteCond %{TIME_SEC} ^(.*)
RewriteRule .* http://%{HTTP_HOST}%{REQUEST_URI}?time=%1 [R,L]

Displaying 1 of 2 files every 30 seconds

This code rewrites requests for http://www.askapache.com/dream/ to /promo.php during the first 30 seconds of every minute, and during the last 30 seconds it instead serves /promo1.php. I came up with this basic example to experiment further on Boosting DreamHost Referrals.

RewriteEngine On
RewriteBase /

RewriteCond %{TIME_SEC} <30
RewriteRule ^dream/?$ /promo.php [L]
RewriteCond %{TIME_SEC} >29
RewriteRule ^dream/?$ /promo1.php [L]

If Server is -3 hours slow

This code instructs apache to serve alternate versions of a webpage depending on the time of day.. one of my clients wanted this feature to be able to show different images (sun + moon) and show different content depending on what part of the day it was. You could also use this with css files to have really easy method of showing your funky side!

Options +FollowSymLinks
RewriteEngine On
RewriteBase /

# 5am > < 8am
RewriteCond %{TIME_HOUR} >02
RewriteCond %{TIME_HOUR} <05
RewriteRule ^index\.html$ /morning/index.html

# 8am > < 4pm
RewriteCond %{TIME_HOUR} >05
RewriteCond %{TIME_HOUR} <13
RewriteRule ^index\.html$ /midday/index.html

# 4pm > < 10pm
RewriteCond %{TIME_HOUR} >13
RewriteCond %{TIME_HOUR} <19
RewriteRule ^index\.html$ /afternoon/index.html

# 10pm > < 5am
RewriteCond %{TIME_HOUR} >19
RewriteCond %{TIME_HOUR} <02
RewriteRule ^index\.html$ /night/index.html

Tags

Comments Welcome

Information is freedom. Freedom is non-negotiable. So please feel free to modify, copy, republish, sell, or use anything on this site in any way at any time ;)

My Online Tools

Popular Articles
Hacking and Hackers

The use of "hacker" to mean "security breaker" is a confusion on the part of the mass media. We hackers refuse to recognize that meaning, and continue using the word to mean someone who loves to program, someone who enjoys playful cleverness, or the combination of the two.
-- Richard M. Stallman


It's very simple - you read the protocol and write the code. -Bill Joy

Except where otherwise noted, content on this site is licensed under a Creative Commons Attribution 3.0 License, just credit with a link.
This site is not supported or endorsed by The Apache Software Foundation (ASF). All software and documentation produced by The ASF is licensed. "Apache" is a trademark of The ASF. NCSA HTTPd.
UNIX ® is a registered Trademark of The Open Group. POSIX ® is a registered Trademark of The IEEE.

+Askapache | htaccess.io | htaccess.guru

Site Map | Contact Webmaster | License and Disclaimer | Terms of Service | @Htaccess

↑ TOPMain