FREE THOUGHT · FREE SOFTWARE · FREE WORLD

Home  »  Htaccess  »  Serve Alternate Content based on Time

by 7 comments

[hide]

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

February 11th, 2007

Comments Welcome

  • bob

    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

  • nico

    christmas logo for your website - I'd like to share this idea I had for changing the logo of a website during a holiday period:

    just adding some Rewrite directives in a .htaccess with time conditions... so no need to worry about overwriting the logo but just produce a new logo and decide previously the time period it should be changed.

    The code:

    #CHRISTMAS LOGO
    #FROM 10 DECEMBER TO 31
    RewriteCond %{TIME_DAY} >09
    RewriteCond %{TIME_DAY} 10
    RewriteRule ^img/logo_common.png$ /img/logo_xmas.png [L,NC]
     
    #FROM JANUAR 1st to 6th
    RewriteCond %{TIME_DAY} >00
    RewriteCond %{TIME_DAY} <07
    RewriteCond %{TIME_MON} <02
    RewriteRule ^img/logo_common.png$ /img/logo_xmas.png [L,NC]

    It works fine, just some improvements I would like to add:
    Actually I'd like to join the two conditions sets in one only but I didn't realize yet how to do it, and also

    RewriteCond %{TIME_DAY} <=07

    seems not working so the hard part reading/understanding numbers related to dates in my code....

    trouble:

    if i use in my thunderbitd client a html signature with a tag image with src pointing to the path of the image redirected by my .htaccess

    i can display signature logo image at composition time.

    But once the mail is delivered the receiver will not see the image.... any idea?

  • Joe Bertram

    Really helpful article.

    I'm trying to leverage this to redirect to a maintenance page during our weekly scheduled maintenance time frame.

    But I'm running into issues where the variables are literally being cached by the client browser when the page first loads and they never get refreshed after that.

    Is there a way to stop the caching of the %{TIME_WDAY} and %{TIME_HOUR} variables?

    Thanks!

    -Joe

    • http://www.askapache.com/ AskApache

      @Joe ~

      You need to be more specific, because as far as I know those are never cached, they are internal apache variables set for each request. One trick you could try is add a non-cacheable element on the first page, like favicon-redirect.ico, and then force it to never be cacheable and always re-validate and proxy-revalidate. Then you force the redirect for requests to it.

      I wrote an article on this blog somewhere about using a very similar but more advanced way to lock down a login page by setting a cookie on the favicon.. maybe check that. And check your HTTP headers.

  • http://URL Name work

    I am new to .htaccess programming and I debugging. I'd like to point out that there can NOT a space char
    after arrow ( > < )

  • http://URL Adieu

    If I want to block EVERYTHING between time 1601 and 1602 What should be the last line of RewriteRule .* - [F]?

    RewriteEngine on
    RewriteCond %{TIME_HOUR}%{TIME_MIN} >1601
    RewriteCond %{TIME_HOUR}%{TIME_MIN} >1602
    RewriteRule .* - [F]
  • http://www.transcorp.com Tim Grissom

    I have this in my htaccess file to serve up a custom 503 page on Sundays:

    # serve specific content on Sunday (Day 0) - custom error page
    RewriteCond %{TIME_WDAY} 0
    RewriteRule ^index.shtml$ /503.shtml

    How do I edit the rewrite rule so this applies to all shtml pages on my site, not just the index.shtml page?

My Online Tools
My Picks
Twitter

  • : Ratpoison, a lean mean WM for Linux (urvxt), I love it
  • : You understand, what I need to know is exactly what happens to the passengers in an elevator when it falls into emptiness - Einstein
  • : It's not process, its content. That's what makes good products. -jobs
  • : Just bought some great BOSE headphones from best buy


Related Articles
Newest Posts

WordPress Development
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. See my article, On Hacking.
-- 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.

| Google+

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

↑ TOPMain