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

Table of Contents

  1. Introduction
    1. Problem
    2. Solution

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

Using TIME_HOUR and TIME_MIN for htaccess RewriteCond - AskApache

Charles Torvalds
6 Jan 2007

  • Site Map WireShark GNU Non-GNU Tor Project cURL TLDP - Documentation
  • Htaccess Files Hacking Htaccess Javascript Linux Optimization PHP Security Shell Scripting WordPress
  • Base64 Image Converter Raw HTTP Header Debugger Graphical ASCII Text Generator Mac Address Vendor Lookup Who Am I – Your IP Information Request Method Security Scanner .htpasswd file Generator Compress CSS DNS Tracer
Copyright © 2025 AskApache
  • Site Map
  • Htaccess Files
  • Hacking
  • Htaccess
  • Javascript
  • Linux
  • Optimization
  • PHP
  • Security
  • Shell Scripting
  • WordPress
  • Base64 Image Converter
  • Raw HTTP Header Debugger
  • Graphical ASCII Text Generator
  • Mac Address Vendor Lookup
  • Who Am I – Your IP Information
  • Request Method Security Scanner
  • .htpasswd file Generator
  • Compress CSS
  • DNS Tracer
Exit mobile version