FREE THOUGHT · FREE SOFTWARE · FREE WORLD

Home » Htaccess »  Using TIME_HOUR and TIME_MIN for htaccess RewriteCond

Using TIME_HOUR and TIME_MIN for htaccess RewriteCond

by Charles Torvalds 2 comments

[hide]

If you want to redirect to a different version of a file based on the time, this code is for you! Please read: Serve Alternate Content based on Time

On this page → [hide]

Problem

Many webmasters use CGI scripts to do simple redirects based on the time of day. CGI incurs alot of overhead when used with SSI includes, even more so if the page is going to be called often.

Solution

Mod_rewrite provides system-variables for exactly this purpose. Using the lexicographic comparisons, we can do time-dependant redirects:

RewriteEngine On
RewriteBase /
RewriteCond %{TIME_HOUR}%{TIME_MIN} >0700
RewriteCond %{TIME_HOUR}%{TIME_MIN} <1900
RewriteRule ^index.html$ /index.day.html
RewriteRule ^index.html$ /index.night.html

This provides content for index.html so that requests done between 7am to 7pm are considered "day", and therefore get redirected to index.day.html, otherwise the page index.night.html is displayed.

.htaccess,apache-htaccess,htacess,htacces,htaccess,apache,rewriterule,rewriteengine,rewritebase,rewriterule,rewritecond,time_hour,time_min,variables

January 6th, 2007

Comments Welcome

  • Phil

    That should be ">19 [OR]", else /night/ will never get used, because it's impossible for a TIME_HOUR to be BOTH greater than 19 AND less than 02.

  • http://Gnarlodious.com/ Gnarlodious

    time-dependant is spelled wrong. I looked for an "Edit" button but found none. Thanks for the hint on time-conditional rewrites.

My Online Tools

Related Articles
Twitter

  • askapache: Today in 1965 DEC announces PDP-8
  • hubail: RT @askapache: Make sure you unplug your Ethernet when leaving the room, or disable wifi
  • askapache: Make sure you unplug your Ethernet when leaving the room, or disable wifi
  • askapache: My servers, and me, are getting annoyed. Fail2ban works fairly well against all the Chinese brute forcing going on
  • askapache: Can't the Chinese stop ordering their hackers to hack us? Ugh
  • askapache: All I want for my bday is a bottle of American whiskey :)
  • askapache: The first Dino fossil wasn't found until 1822, we sure are young

My Picks
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.

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

↑ TOPMain