FREE THOUGHT · FREE SOFTWARE · FREE WORLD

Home » Htaccess » Serve Alternate Content based on Time

Serve Alternate Content based on Time

Serve Alternate Content based on Time Apache mod_rewrite code to serve alternate versions of a file depending on the server time - week, month, hour, year, second

Serve Alternate Content based on Time

February 11th, 2007

TIME_DAY Time RewriteCondTime-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

http://www.askapache.com/htaccess/time_hour-rewritecond-time.html#comments

Reader Comments

  1. bob ~December 5, 2011 @ 5:13 pm
    Great article, thank you. However I am struggling adapting it. Might you know how I can achieve the following? I wish to temporarily block visitors (IPs) after 5 minutes, then allow them access again after a few hours. Any idea how I can do this? Many thanks, Bob

Add Comment!

Leave a Reply

Your email address will not be published.


Google +

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.

Site Map | Contact Webmaster | Glossary | License and Disclaimer | Terms of Service |

↑ TOPMain