Time-dependent URL rewriting

This post explains how to configure time-dependent URL rewriting rules by using IIS URL Rewrite 2.0. Time-dependent URL rewriting may be useful when you want to rewrite or redirect HTTP requests based on the time of day or when you need to take the entire site or some parts of the site offline for a scheduled period of time.

The rewrite rules are not time-aware, but it is relatively easy to add the time-dependent functionality by using URL Rewrite 2.0 extensibility. Follow the steps below to add a scheduler rewrite provider for URL Rewrite module.

Step 1: Download the provider source code and compiled .NET assembly from the link below and unpack it into some temporary location:

RewriteScheduler provider for URL Rewrite 2.0

Disclaimer: The code and the .NET assembly contained in this package are provided by me and not by Microsoft. The are not officially supported by Microsoft. Use them at your own risk.

Step 2: Add the .NET assembly to the Global Assembly Cache by using this command:

gacutil.exe /if C:\Some\Temporary\Location\RewriteScheduler.dll 

Step 3: Register the provider instance with URL Rewrite module. In this example the provider instance will be used to specify the day time hours.

Step 4: Configure the time ranges when the scheduler is active. Specify multiple comma-separated time ranges by using 24 hour format.

Step 5: Repeat steps 3 and 4 to register another provider instance that is used to specify night time hours:

Now you can create rewrite rules that perform different actions based on the time of day. For example to serve different pages depending on whether it is day or night use the following rules:

<rule name="Day-time rewrite" patternSyntax="Wildcard">
  <match url="page.html" />
  <conditions>
    <add input="{DayTimeScheduler:void}" pattern="ON" />
  </conditions>
  <action type="Rewrite" url="page.day.html" />
</rule>
<rule name="Night-time rewrite" patternSyntax="Wildcard">
  <match url="page.html" />
  <action type="Rewrite" url="page.night.html" />
</rule>

To take the site offline during the night time use the following rule:

<rule name="Site offline overnight" patternSyntax="Wildcard" stopProcessing="true">
  <match url="*" />
  <conditions>
    <add input="{NightTimeScheduler:void}" pattern="ON" />
  </conditions>
  <action type="CustomResponse" statusCode="503"
                         statusReason="Site is offline"
                         statusDescription="Site is temporarily offline" />
</rule>

9,477 views

ruslany on April 4th 2010 in URLRewrite

PoorFairAverageGoodExcellent (5 votes, average: 4.20 out of 5)

One Response to “Time-dependent URL rewriting”

  1. Gravatar ImageAneetha responded on 07 Feb 2012 at 4:59 am #

    Hi,
    I am new to IIS, trying to implement ISAPI to redirect requests based on cookies. Is it possible to do it in Rewrite module instead of ISAPI. To check cookie name and expiration and based on that redirect the url. If yes, please guide me as to how to implement the same.

    Thank you.

Trackback URI | Comments RSS

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

XML Markup: If You want to add XML code to the comment please XML encode it first, otherwise the code will not show up.

Recently Published Articles