Click here to Skip to main content
15,881,882 members
Articles / Web Development / ASP.NET
Article

A New Generation of URL Rewriters ?

Rate me:
Please Sign up or sign in to vote.
4.60/5 (10 votes)
18 Apr 2008Ms-PL7 min read 90.5K   356   61   32
A URL Rewriter/ Redirector which utilizes new and advanced IIS7 Integrated ASP.NET Pipeline feature


Introduction

I have always had problems finding a suitable ASP.NET URL Rewriter that fully meets my needs, so about one year ago, after much frustration with attempting to follow third party open-source code and related poor documentation, I decided to write my own. I have since repackaged this URL Rewriter/ Redirector into the name of nUrlRewriter Version 2. nUrlRewriter has been posted on popular download sites, such as codeplex.com and code.msdn.microsoft.com

nUrlRewriter is a ASP.NET Http Module written in managed C# code. nUrlRewriter examines incoming Http requests and applies user defined criteria which may result in a Http request being redirected or rewritten. Web pages within existing web sites are often archived or retired, however many Internet based hyperlinks may exist for such web pages. nUrlRewriter solves this problem by providing a facility which can easily redirect or rewrite such Http requests to other web site web pages or web applications. For example, a discontinued product web page may be redirected to a general product category web page. nUrlRewriter differentiates itself from other redirectors/rewriters in that nUrlRewriter also supports the IIS7 Integrated Pipeline, enabling nUrlRewriter to redirect/rewrite any incoming web application URL supported by the IIS7 web server, such as but not limited to native HTML applications (htm, html), classic ASP applications (asp), PHP applications (php) as well as ASP.NET (aspx) applications.

Incoming Http requests which are redirected are returned to the originating browser with a status code of either 301 (permanent) or 302 (temporary) to indicate that the requested web page has been moved to a new target URL provided to the browser. the browser will then issue a new Http request for the new URL. Http status code 301 indicates that the URL has been permanently moved and the browser should use the new URL in any new Http requests. Http status code 302 indicates that the URL has been temporarily moved and the browser should use the new URL only for the outstanding Http request.

Incoming Http requests which are rewritten are rewritten to a different URL location within IIS. Since the originating browser is not informed of the URL rewrite, the browser URL address bar will continue to display the originating URL before the URL rewrite.

nUrlRewriter works equally as well with IIS5 and IIS6.

Background

Clearly, with an all new internal architecture and with new enriched Remote Administration and Feature Delegation enhancements, IIS7 opens up a new generation of web hosting and demands a new generation of support infrastructure tools as well; such as Url Rewriters/ Redirectors.

nUrlRewriter not only works well in an IIS5 or IIS6 environment, but nUrlRewriter takes advantage of the new IIS7 Integrated ASP.NET Pipeline to simplify communications between native HTML applications (.htm, .html), classic ASP (.asp), PHP applications (.php) and ASP.NET (.aspx).

nUrlRewriter can be used for the following:

  1. To redirect/rewrite in case of retired or archived web pages.
  2. To rewrite from a friendly URL format to an internal URL format.
  3. To redirect to secondary folder locations; for example language specific web site content may reside in separate folders, or a single IP address may be hosting multiple web sites, with the domain specific content residing in sub-folders.
  4. To provide for URL specific robots.txt files. For example if a single IP address is supporting multiple web sites, then web site specific robots.txt files can be returned when requested.

Redirecting Incoming Http Requests

Redirecting Incoming Http Requests

Rewriting Incoming Http Requests

Rewriting Incoming Http Requests

Using with IIS Version 5 or 6

nUrlRewriter executes in the form of a Http Module, so nUrlRewriter must be defined in the web.config <system.web> <httpModules> configuration section, as illustrated below:

 1:    <system.web>
 2:      <httpModules>
 3:        <add name="nUrlRewriter"
 4:             type="nUrlRewriter.HttpModule,
 5:                    nUrlRewriter,
 6:                    Version=2.0.0.0,
 7:                    Culture=neutral,
 8:                    PublicKeyToken=741b921e11e02781"/>
 9:      </httpModules>
10:    </system.web>

To configure specific nUrlRewriter options, the nUrlRewriter web.config configuration section must now be declared, as illustrated below:

1:    <configSections>
2:      <section name="nUrlRewriter"
3:               type="nUrlRewriter.Configuration2.Configuration,
4:                      nUrlRewriter,
5:                      Version=2.0.0.0,
6:                      Culture=neutral,
7:                      PublicKeyToken=741b921e11e02781"/>
8:    </configSections>

Once the nUrlRewriter Http Module is defined and the nUrlRewriter configuration section is declared, the nUrlRewriter specific configuration section must be defined within the web.config.

I am currently supporting two web sites; my corporate web site - www.sanibellogic.com and my personal web site - www.plippard.com. The entry point for both sites is at the root level (wwwRoot), with a common IP address. Any future additional web sites being hosted will also share the same IP address. The root level of my web site redirects based on domain name, and then more specific redirect/ rewrite logic is applied at the sub-folder (or actual web site sub-folder) level. Essentially, at the wwwRoot level, sanibellogic.com domains are redirected to the “/SL” sub-folder, and plippard.com domains are redirected to the “/PGL” sub-folder. Because these sites are currently being hosted on IIS6, IIS7 specific features are not currently used. IIS7 specific features will be discussed later in this article.

The following exhibit shows the root level nUrlRewriter configuration section for achieving the above discussed domain redirection:

 1:  <?xml version="1.0" encoding="utf-8" ?>
 2:  <nUrlRewriter xmlns="http://schemas.sanibellogic.com/nUrlRewriter/config/2/0/0/0"
 3:                enabled="true"
 4:                trace="false">
 5:    <urls>
 6:      <clear/>
 7:
 8:      <add name="RuleSanibelLogic"
 9:           action="redirect"
10:           ignoreCase="true"
11:           redirectType="temporary"
12:           transformType="RegExReplace"
13:           fromScope="absolute"
14:           from="^http(?&lt;SSL&gt;[s]?)://(www.)?sanibellogic.com/(?&lt;WebPage&gt;(.*))$"
15:           to="http${SSL}://www.sanibellogic.com/sl/${WebPage}" />
16:
17:      <add name="RulePLippard"
18:           action="redirect"
19:           ignoreCase="true"
20:           redirectType="temporary"
21:           transformType="RegExReplace"
22:           fromScope="absolute"
23:           from="^http(?&lt;SSL&gt;[s]?)://(www.)?plippard.com/(?&lt;WebPage&gt;(.*))$"
24:           to="http${SSL}://www.plippard.com/pgl/${WebPage}" />
25:
26:    </urls>
27:  </nUrlRewriter>

The above configuration section exhibit primarily examines the incoming domain name, transforms the absence of a sub-domain to "www." and then redirects the incoming request to a defined ASP.NET application sub-folder (either /SL or /PGL) based on whether the domain is "sanibellogic.com" or "plippard.com". All sub-folder or query string content following the domain name is also appended to the redirected request.

Once the incoming request is intercepted and redirected to the proper ASP.NET application sub-folder, nUrlRewriter (operating in the context of the /SL or /PGL sub-folder) will again have an opportunity to intercept and redirect or rewrite the new incoming request, . The exhibit below shows the ASP.NET application folder level configuration section used by nUrlRewriter when operating in the context of the /SL sub-folder:

  1:  <nUrlRewriter xmlns="http://schemas.sanibellogic.com/nUrlRewriter/config/2/0/0/0"
  2:                enabled="true"
  3:                trace="false">
  4:
  5:    <urls>
  6:      <clear/>
  7:
  8:      <!-- Rewrite one time DownloadMe.aspx root level DownloadMe page to new location,
  9:            Note: rewriteType="transferrequest" requires IIS7 Integrated Pipeline mode -->
 10:      <add name="RuleDownloadMe"
 11:           action="rewrite"
 12:           rewriteType="rewritePath"
 13:           ignoreCase="true"
 14:           transformType="RegExReplace"
 15:           from="~/DownloadMe.aspx(?&lt;QStrings&gt;(.*))$"
 16:           to="~/Common/S/DownloadMe.aspx${QStrings}" />
 17:
 18:      <!-- Rewrite ECartDownload.aspx web page to new location,
 19:            Note: rewriteType="transferrequest" requires IIS7 Integrated Pipeline mode -->
 20:      <add name="RuleECartDownload"
 21:           action="rewrite"
 22:           rewriteType="rewritePath"
 23:           ignoreCase="true"
 24:           transformType="RegExReplace"
 25:           from="~/Common/ECartDownload.aspx(?&lt;QStrings&gt;(.*))$"
 26:           to="~/Common/S/ECartDownload.aspx${QStrings}" />
 27:
 28:      <!-- Rewrite PaypalIPN web page to new location,
 29:            Note: rewriteType="transferrequest" requires IIS7 Integrated Pipeline mode -->
 30:      <add name="RulePaypalIPN"
 31:           action="rewrite"
 32:           rewriteType="rewritePath"
 33:           ignoreCase="true"
 34:           transformType="RegExReplace"
 35:           from="~/Common/PaypalIPN.aspx(?&lt;QStrings&gt;(.*))$"
 36:           to="~/Common/S/PaypalIPN.aspx${QStrings}" />
 37:
 38:      <!-- Redirect one time Products.aspx root level Products page to new location -->
 39:      <add name="RuleProducts"
 40:           action="redirect"
 41:           ignoreCase="true"
 42:           redirectType="permanent"
 43:           transformType="RegExReplace"
 44:           from="~/Products.aspx(?&lt;QStrings&gt;(.*))$"
 45:           to="~/Common/Products.aspx${QStrings}" />
 46:
 47:      <!-- Redirect discontinued Products.aspx DotNetNuke SSLRedirect queries -->
 48:      <add name="RuleDotNetNuke00200"
 49:           action="redirect"
 50:           ignoreCase="true"
 51:           redirectType="permanent"
 52:           transformType="RegExReplace"
 53:           from="~/Common/Products.aspx\?Cat=DotNetNuke&amp;PLong=00200$"
 54:           to="~/Common/Products.aspx?Cat=ASP.NET&amp;PLong=00401" />
 55:
 56:      <!-- Redirect discontinued Products.aspx DotNetNuke SSLRedirect SDK queries -->
 57:      <add name="RuleDotNetNuke00201"
 58:           action="redirect"
 59:           ignoreCase="true"
 60:           redirectType="permanent"
 61:           transformType="RegExReplace"
 62:           from="~/Common/Products.aspx\?Cat=DotNetNuke&amp;PLong=00201$"
 63:           to="~/Common/Products.aspx?Cat=ASP.NET&amp;PLong=00402" />
 64:
 65:      <!-- Redirect discontinued Products.aspx DotNetNuke SmartNews queries -->
 66:      <add name="RuleDotNetNuke00202"
 67:           action="redirect"
 68:           ignoreCase="true"
 69:           redirectType="permanent"
 70:           transformType="RegExReplace"
 71:           from="~/Common/Products.aspx\?Cat=DotNetNuke&amp;PLong=00202$"
 72:           to="~/Common/Products.aspx?Cat=ASP.NET" />
 73:
 74:      <!-- Redirect discontinued Products.aspx DotNetNuke SmartNews SDK queries -->
 75:      <add name="RuleDotNetNuke00203"
 76:           action="redirect"
 77:           ignoreCase="true"
 78:           redirectType="permanent"
 79:           transformType="RegExReplace"
 80:           from="~/Common/Products.aspx\?Cat=DotNetNuke&amp;PLong=00203$"
 81:           to="~/Common/Products.aspx?Cat=ASP.NET" />
 82:
 83:      <!-- Redirect discontinued Products.aspx DotNetNuke HttpCompressionAgent queries -->
 84:      <add name="RuleDotNetNuke00204"
 85:           action="redirect"
 86:           ignoreCase="true"
 87:           redirectType="permanent"
 88:           transformType="RegExReplace"
 89:           from="~/Common/Products.aspx\?Cat=DotNetNuke&amp;PLong=00204$"
 90:           to="~/Common/Products.aspx?Cat=ASP.NET&amp;PLong=00400" />
 91:
 92:      <!-- Redirect discontinued Products.aspx DotNetNuke product category -->
 93:      <add name="RuleDotNetNuke"
 94:           action="redirect"
 95:           ignoreCase="true"
 96:           redirectType="permanent"
 97:           transformType="RegExReplace"
 98:           from="~/Common/Products.aspx\?Cat=DotNetNuke$"
 99:           to="~/Common/Products.aspx?Cat=ASP.NET" />
100:
101:    </urls>
102:
103:  </nUrlRewriter>

As can be seen from the above nUrlRewriter configuration sections, nUrlRewriter can be easily extended without code source changes, simply by defining the actions required in the configuration section.

Using with IIS Version 7 (Windows Server 2008 and Vista)

Once released, I fully expect to take advantage of the much publicized and anticipated Windows Server 2008 IIS7 features. When one plans on utilizing the more advanced IIS7 features, and one’s web application is defined to IIS7 as executing in “Integrated ASP.NET Pipeline” mode then nUrlRewriter is defined in a slightly different manner within the web.config. The nUrlRewriter Http Module is now defined within the new <system.webserver> configuration section, illustrated below:

 1:    <system.webServer>
 2:      <modules>
 3:        <add name="nUrlRewriter"
 4:             type="nUrlRewriter.HttpModule,
 5:                    nUrlRewriter,
 6:                    Version=2.0.0.0,
 7:                    Culture=neutral,
 8:                    PublicKeyToken=741b921e11e02781"/>
 9:      </modules>
10:    </system.webServer>

The nUrlRewriter configuration section continues to be declared and defined as with IIS5/6, illustrated as follows:

1:    <configSections>
2:      <section name="nUrlRewriter"
3:               type="nUrlRewriter.Configuration2.Configuration,
4:                      nUrlRewriter,
5:                      Version=2.0.0.0,
6:                      Culture=neutral,
7:                      PublicKeyToken=741b921e11e02781"/>
8:    </configSections>

One of the nUrlRewriter configuration options available with IIS7 is the ability to rewrite/ redirect between any web application supported by the IIS7 web server, such as but not limited to native HTML applications (htm, html), classic ASP applications (asp), PHP applications (php) as well as ASP.NET (aspx) applications. To achieve this objection (for example), one would configure the nUrlRewriter configuration section as follows:

 1:  <nUrlRewriter xmlns="http://schemas.sanibellogic.com/nUrlRewriter/config/2/0/0/0"
 2:                              enabled="true"
 3:                              trace="false">
 4:
 5:      <topLevelExtensions>
 6:          <clear/>
 7:          <add extension="asp" />
 8:          <add extension="aspx" />
 9:          <add extension="htm" />
10:          <add extension="html" />
11:          <add extension="php" />
12:      </topLevelExtensions>
13:
14:      <urls>
15:          <clear/>
16:
17:          <!-- Redirect WordPress blog URL, which was a sub-folder within an ASP.NET App
18:                      to new ASP.NET BlogEngine.Net sub-folder -->
19:          <add name="RuleBlog"
20:                   action="redirect"
21:                   from="~/support/wpblog/(.+)"
22:                   to="~/support/beblog/" />
23:      </urls>
24:
25:  </nUrlRewriter>

Please note that the topLevelExtensions tag defines the candidate web page extensions, required for PHP application visibility in this example.

With IIS7, another common use of nUrlRewriter will be support for multiple robots.txt files. The robots.txt file is ordinarily required to be at the root level of a web site. The following nUrlRewriter configuration section illustrates rewriting a web request for a root level robots.txt file to a sub-folder resident robots.txt, making its sub-folder location transparent to the invoking search engine. The to attribute MUST be transformed into a relative URL location, which starts with "~/", because both the System.Web.HttpServerUtility.TransferRequest and System.Web.HttpContext.RewritePath methods require a relative URL path. Please note that the "txt" extension must also be included within the topLevelExtensions collection.

 1:    <nUrlRewriter xmlns="http://schemas.sanibellogic.com/nUrlRewriter/config/2/0/0/0"
 2:                  enabled="true"
 3:                  trace="false">
 4:
 5:          <topLevelExtensions>
 6:              <clear/>
 7:                  <add extension="asp" />
 8:                  <add extension="aspx" />
 9:                  <add extension="htm" />
10:                  <add extension="html" />
11:                  <add extension="php" />
12:                  <add extension="txt" />
13:
14:          </topLevelExtensions>
15:
16:          <urls>
17:              <clear/>
18:
19:              <!-- Based on incoming domain of SanibelLogic.com, rewrite to a -->
20:              <!-- sub-folder location for the robots.txt file -->
21:              <add name="RuleSanibelLogicRobots"
22:                      action="rewrite"
23:                      rewriteType="transferRequest"
24:                      fromScope="absolute"
25:                      from="^http(?&lt;SSL&gt;[s]?)://(www.)?sanibellogic.com/(?&lt;robotsFile&gt;(robots.txt))$"
26:                      to="~/SL/$(robotsFile)" />
27:
28:              <!-- Based on incoming domain of PLippard.com, rewrite to a -->
29:              <!-- sub-folder location for the robots.txt file -->
30:              <add name="RulePLippardRobots"
31:                      action="rewrite"
32:                      rewriteType="transferRequest"
33:                      fromScope="absolute"
34:                      from="^http(?&lt;SSL&gt;[s]?)://(www.)?plippard.com/(?&lt;robotsFile&gt;(robots.txt))$"
35:                      to="~/PGL/$(robotsFile)" />
36:
37:          </urls>
38:    </nUrlRewriter>

A URL rewriting/ redirecting web component is essential for effective management of multiple as well as single web sites. nUrlRewriter utilizes new and advanced IIS7 Integrated ASP.NET Pipeline features to ensure that all web applications, such as but not limited to native HTML applications (htm, html), classic ASP applications (asp), PHP applications (php), and ASP.NET applications (aspx) are integrated in an effective manner. nUrlRewriter also utilizes regular expressions and a flexible configuration design to ensure ease of expansion without source code changes. nUrlRewriter documentation in .chm help file form is available with the VS.NET 2008 source code project download.

History

18-Apr-2008 - Initial Release (although I have been executing in production under a different name for one year)

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)


Written By
Web Developer Sanibel Logic LLC
United States United States
Philip Lippard is Chief Technical Officer at Sanibel Logic LLC. Sanibel Logic ( http://www.sanibellogic.com ) is focused on providing software products and technical consulting to selected clients; leveraging a wide range of Microsoft Web based and Windows client/server enterprise technologies. Products and services have been provided to licensees in thirty six (36) countries.

Comments and Discussions

 
Questiontwo identical internal calls from different origins, one is redirected other is not Pin
RickABS12-Sep-20 7:01
RickABS12-Sep-20 7:01 
QuestionProblem with folder level rewriting Pin
GrantSt21-Aug-08 6:40
GrantSt21-Aug-08 6:40 
AnswerRe: Problem with folder level rewriting Pin
Philip Lippard27-Aug-08 12:40
Philip Lippard27-Aug-08 12:40 
GeneralFor Simplest Way of writing URL Rewriting Pin
DotNetGuts24-Jul-08 10:41
DotNetGuts24-Jul-08 10:41 
QuestionRewriting a URL [modified] Pin
jcsernik30-May-08 11:36
jcsernik30-May-08 11:36 
GeneralRe: Rewriting a URL Pin
Philip Lippard31-May-08 2:30
Philip Lippard31-May-08 2:30 
GeneralRe: Rewriting a URL Pin
jcsernik9-Jun-08 8:06
jcsernik9-Jun-08 8:06 
GeneralRe: Rewriting a URL Pin
Philip Lippard9-Jun-08 8:17
Philip Lippard9-Jun-08 8:17 
GeneralRe: Rewriting a URL Pin
Philip Lippard9-Jun-08 8:39
Philip Lippard9-Jun-08 8:39 
GeneralQuestion Pin
Member 380861427-May-08 1:18
Member 380861427-May-08 1:18 
GeneralRe: Question Pin
Philip Lippard27-May-08 1:46
Philip Lippard27-May-08 1:46 
GeneralRewriting asp and asp.net URLs Pin
buosc6-May-08 7:19
buosc6-May-08 7:19 
GeneralRe: Rewriting asp and asp.net URLs Pin
Philip Lippard9-May-08 13:58
Philip Lippard9-May-08 13:58 
GeneralRe: Rewriting asp and asp.net URLs Pin
buosc12-May-08 8:34
buosc12-May-08 8:34 
GeneralRe: Rewriting asp and asp.net URLs Pin
Philip Lippard12-May-08 14:36
Philip Lippard12-May-08 14:36 
QuestionRewriting folders Pin
Jenya Y.22-Apr-08 11:14
Jenya Y.22-Apr-08 11:14 
GeneralRe: Rewriting folders Pin
Philip Lippard22-Apr-08 19:01
Philip Lippard22-Apr-08 19:01 
QuestionRe: Rewriting folders Pin
Jenya Y.22-Apr-08 20:40
Jenya Y.22-Apr-08 20:40 
AnswerRe: Rewriting folders Pin
Philip Lippard30-Apr-08 8:25
Philip Lippard30-Apr-08 8:25 
QuestionLooks good Pin
stixoffire18-Apr-08 20:47
stixoffire18-Apr-08 20:47 
GeneralRe: Looks good Pin
mischa_k22-Apr-08 5:12
mischa_k22-Apr-08 5:12 
GeneralRe: Looks good Pin
Philip Lippard22-Apr-08 19:03
Philip Lippard22-Apr-08 19:03 
GeneralNice work Pin
Viral Upadhyay18-Apr-08 20:45
Viral Upadhyay18-Apr-08 20:45 
I didnt test it but its new feature and i must say will be very useful to developer like me.
Very Good work Rose | [Rose]

Viral
YahooID : just_viral

GeneralGood! question Pin
zhaojicheng18-Apr-08 18:56
zhaojicheng18-Apr-08 18:56 
GeneralRe: Good! question Pin
leppie18-Apr-08 22:21
leppie18-Apr-08 22:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.