asp:Feature
LANGUAGES:
C#
ASP.NET
VERSIONS: ALL
More Smart SEO
URL Rewriting and Redirection Using the IIS 7.0 URL Rewrite Module
By Jeffrey Hasan
Search Engine Optimization (SEO) has become the major
driver for Uniform Resource Locator (URL) rewriting in Web sites. URL rewriting
involves intercepting an incoming HTTP request and remapping the original URL
request to an alternate URL. URL redirection publicly notifies the site visitor
of a temporary or permanent change in the URL they requested.
URL rewriting and redirection are not the same thing. URL
rewriting recognizes URL pattern matches, then rewrites the incoming URL to a
different internal URL. The action of rewriting a URL can be done silently
without notifying the visitor of an official redirect, using HTTP status codes
301 and 302 (for permanent and temporary redirection, respectively). URL
redirection employs the same pattern matching process as URL rewriting, but
then takes the process one step further by redirecting the site visitor and sending
them an official HTTP status code.
For instance, here s an example of an SEO-friendly URL for
an HP printer on an e-commerce site:
http://www.jeffshop.com/printers/hp/P1006/
Here s an alternate URL for the same product, constructed
in a development-friendly format:
http://www.jeffshop.com/printers.aspx?mfr=hp&model=p1006
URL rewriting is useful when you want to expose
SEO-friendly URLs, but map them internally to a different URL format or site
structure. URL redirection is useful when you want to combine the URL rewriting
pattern matching process with public notification to the site visitor of a
change in the URL.
URL rewriting in IIS 5 and 6 is a manual coding and
configuration effort, although third-party tools make the task easier. The task
is much easier in IIS 7.0, courtesy of the newly released Microsoft URL Rewrite
module, which is available as a free download from http://www.iis.net/.
The focus of this article is on URL rewriting and redirection using the IIS 7.0
URL Rewrite module.
Note: This article
focuses on URL rewriting and redirection using IIS 7.0. For a complete
discussion of SEO and URL rewriting in previous versions of IIS, please consult
my previous article, Smart SEO: URL Rewriting Using ASP.NET and IIS 6.0 , in
the March 2009 issue of asp.netPRO.
Introduction to the IIS 7.0 URL Rewrite Module
The IIS 7.0 URL Rewrite module provides rules-based URL
rewriting and redirection capabilities. The module coordinates with IIS 7.0 to
intercept URL requests before they are processed by the Web server. Rewrite
rules can be configured using a forms-based utility that is accessible from the
IIS management console. Or, you can directly edit rules in the Web application s
web.config file. The URL Rewrite module provides several features, as outlined
in Figure 1.
|
Feature |
Description |
|
Rules-based URL rewriting engine |
The core function of the module is to process URL
rewrites using configured rules. The module also supports rule actions that
follow rewrites, including redirects. Rewrite rules can be established
globally, or for a specific Web application, where the rules are encoded
within the web.config file. Rewrite rules establish the mappings between
requested URLs and their rewritten replacement URLs. |
|
GUI support |
The module provides graphical user interface screens to
configure and test URL rewrite rules, and their follow-up actions. It also
provides screens to manage rewrite rules and maps. |
|
Rewrite maps |
Rewrite maps group sets of rewrite rules. |
|
Failed request tracing support |
A feature of IIS 7.0 that helps you troubleshoot failed
URL rewrites. |
Figure 1: Features
of the IIS 7.0 URL Rewrite module.
This article will not cover every available feature in the
URL Rewrite module, but rather will cover two important features that will help
you get comfortable with using this tool. The features we ll cover are:
§ Create a URL rewriting rule using regular
expressions.
§ Rewrite and redirect a URL using pattern
matching by example.
The URL Rewrite module is versatile, and we ll demonstrate
how you can use the tool with both regular expressions and auto-generated
pattern matches, which the tool generates for you based on examples you
provide.
Installing the URL Rewrite Module
You can download the URL Rewrite module from http://www.iis.net/downloads/default.aspx?tabid=34&g=6&i=1691.
The module will install on both 32-bit and 64-bit versions of the Microsoft
Vista operating system (as well as Windows Server 2008). You must have
administrator privileges to install the module. When the installation is
complete, restart your machine, then browse the IIS management console; you ll
see a new icon entitled URL Rewrite , as shown in Figure 2.

Figure 2: The URL Rewrite module under IIS 7.0 management console.
Using Visual Studio 2005 with Windows Vista and IIS 7.0
The code and configuration examples in this article are
built on Windows Vista and IIS 7.0 using Visual Studio 2008. However, many developers
have yet to upgrade from Visual Studio 2005. You can continue to use Visual
Studio 2005 on Windows Vista, but you will need to make some minor
configuration updates in order to run Web applications under IIS 7.0 using your
machine s http://localhost Web server root.
First, make sure you have IIS 7.0 installed on your
Windows Vista machine, because it is not installed by default. To do this, go
to Control Panel | Programs and Features and select Turn Windows features on
and off from the sidebar. You need to expand the IIS 6 Management
Compatibility icon and select most of the features, as shown in Figure 3.

Figure 3: Set IIS 6.0 management compatibility.
If you are an advanced user, you also can select the IIS
Management Scripts and Tools and IIS Management Service features, but these are
not required for minimum Visual Studio 2005 compatibility.
Second, if you want to create IIS 7.0 managed Web
applications, you ll need to run Visual Studio as an administrator when you
launch the application from the Programs menu. Figure 4 shows which menu option
you need to select.

Figure 4: Run Visual Studio as an administrator.
If you don t run Visual Studio as an administrator, you ll
still be able to create Web sites that run under the local ASP.NET Web server
at http://localhost:PortNumber.
URL Rewriting Using Rules with Regular Expressions
This first example will show you how to use the URL
Rewriting module and regular expressions to map an SEO-friendly URL to the
actual URL. The SEO-friendly URL is:
http://localhost/URLRewrite/printers/hp/P1006/
The actual URL is:
http://localhost/URLRewrite/printers.aspx?mfr=hp&model=p1006
First, create a simple ASP.NET Web application in Visual
Studio, named URLRewrite, and configure it to run under IIS 7.0. Add a single
ASPX page named printers.aspx and copy in to the page the code shown in Figure 5.
<%@ Page Language="C#"
AutoEventWireup="true"
CodeFile="printers.aspx.cs"
Inherits="printers" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta
http-equiv="Content-Type" content="text/html;
charset=utf-8" />
<title>URL Rewrite
Module Test</title>
</head>
<body>
<h1>Printer Product
Page</h1>
<table>
<tr>
<td>You
Requested:</td>
<td><%=
Request.ServerVariables["HTTP_X_ORIGINAL_URL"]
%></td>
</tr>
<tr>
<td>You Were
Served:</td>
<td><%=
Request.ServerVariables["SCRIPT_NAME"] + "?"
+
Request.ServerVariables["QUERY_STRING"] %></td>
</tr>
</table>
</body>
</html>
Figure 5: URL
Rewrite Module test page.
Next, we ll create a rule that pattern matches the
incoming SEO-friendly URL and rewrites the URL to instruct the Web server to
serve the intended Web page. First, browse the new Web site named URLRewrite
under the IIS management console and double-click the URL Rewrite icon. Next,
click the Add Rules link to open the available rule templates. Select Blank rule,
as shown in Figure 6. Next, fill in the rules property page, as shown in Figure
7.

Figure 6: URL Rewrite rule templates.

Figure 7: URL Rewrite rules property
page.
The name of the rule is PrinterRewrite and the regular
expression pattern match for the incoming rules is:
^printers/([a-z]+)/([0-9a-z]+)
You can click the Test pattern button to test an example,
as shown in Figure 8. The example being tested here is the meaningful substring
of the full Web site URL:
printers/hp/p1006
Finally, apply the changes and you ll see the new rule
listed in the rules summary screen. In Visual Studio, switch over to the Web
application s web.config file and you ll see that a new section has been added
to the web.config file (see Figure 9).

Figure 8: Test a URL Rewrite pattern
match expression.
<system.webServer>
<rewrite>
<rules>
<rule
name="PrintersRedirect">
<match
url="^printers/([a-z]+)/([0-9a-z]+)" />
<action
type="Rewrite" url="printers.aspx?mfr={R:1}
&amp;model={R:2}""
/>
</rule>
</rules>
</rewrite>
</system.webServer>
Figure 9: URL
Rewrite rule in the web.config file.
To test the URL Rewrite rule, simply browse to the
URLRewrite test application using this SEO-friendly URL:
http://localhost/URLRewrite/printers/hp/P1006/
The Web page will display the following:
Printer Product
Page
You Requested: /URLRewrite/printers/hp/p1006
You Were Served:
/URLRewrite/printers.aspx?mfr=hp&model=p1006
Notice that the original SEO-friendly URL remains in the
browser URL field, which is consistent because the rewriting has been done
internally. The Web server has been instructed to serve the correct page, but
no official redirect is issued back to the client.
URL Redirection Using Rules
Redirection is easy to accomplish using the same rule
manager. From an SEO perspective, you want the incoming URL requests to be
formatted as simply as possible, while preserving your flexibility to serve
pages using alternate URLs. So, typically, you would not want to redirect the
user, you would simply want to rewrite the incoming URL so the Web server knows
where to look to service the request.
Redirection implies there is a structural change to your
Web site that you want the outside world to know about. More importantly, a
change that you want search engines to pick up on. Redirection is correctly
accomplished when the Web server returns an HTTP status code 301, which
indicates to search engine spiders that a permanent redirection has taken
place, and that the link equity and page ranking of the former page should be
transferred to the newer page. My previous article on SEO (in the March 2009
issue of asp.netPRO) explains in
detail why you should never issue HTTP status code 302 temporary redirects.
URL redirection is simple using the URL Rewrite module.
You create a similar type of pattern matching rule that we created in the
previous section, but you add an additional instruction on the rule property
page to issue a redirect to the site visitor.
The URL Rewrite module provides a rule template named User
friendly URL, which you can select from the rule templates page shown in Figure
6. This rule template allows you to specify incoming URLs by example, rather
than by regular expression, so you avoid having to create complicated regular
expressions. Figure 10 illustrates this.

Figure 10: URL Redirection using the
User friendly URL rule template.
In this example we want incoming SEO-unfriendly URLs to be
mapped to their SEO-friendly alternatives. In this case, we want:
/URLRewrite/printers.aspx?mfr=hp&model=p1006
to be mapped to:
/URLRewrite/printers/hp/p1006
As Figure 10 shows, simply enter an example of the
SEO-unfriendly URL, then select from the drop-down list the corresponding URL to
which you want it to be mapped. The Substitution URL textbox automatically
updates. Finally, check the Create corresponding redirect rule checkbox and
click OK. The rule manager screen will now list these two additional rules:
§ RewriteUserFriendlyURL1
§ RedirectUserFriendlyURL1
Next, switch to the test application s web.config file and
you ll see that additional rules have been added to the web.config file (see
Figure 11).
<rule name="RedirectUserFriendlyURL1"
stopProcessing="true">
<match
url="^printers\.aspx$" />
<conditions>
<add input="{QUERY_STRING}"
pattern="^mfr=([^=&]+)
&model=([^=&]+)$"
/>
</conditions>
<action
type="Redirect" url="{C:1}/{C:2}"
appendQueryString="false"
redirectType="Permanent" />
</rule>
<rule name="RewriteUserFriendlyURL1" stopProcessing="true">
<match
url="^([^/]+)/([^/]+)/?$" />
<action
type="Rewrite"
url="printers.aspx?mfr={R:1}&model={R:2}" />
</rule>
Figure 11: URL
Rewrite rule in the web.config file.
Notice that the redirect type is permanent, which will
issue an HTTP status code 301. Finally, open a browser window and type in the
SEO-unfriendly URL:
http://localhost/URLRewrite/printers.aspx?mfr=hp&model=p1006
You ll find the URL has been redirected to:
http://localhost/URLRewrite/printers/hp/P1006/
Conclusion
SEO-driven URL rewrites are essential for preserving
hard-earned page rank and link equity for Web pages. The URL Rewrite module is
a useful tool that will save you time in creating rewrite and redirection rules
for Web sites that run under IIS 7.0. The tool has several useful features that
we did not touch on, including the ability to return a custom response, and the
ability to create rule maps that group multiple pattern matching rules
together. For more information on the tool, review the online documentation and
examples at http://www.iis.net/.
Source code accompanying this article is available for download.
Jeffrey Hasan, MCSD, is Senior VP of Strategic Consulting Services at Axis Technical Group, Inc. (http://www.axistechnical.com). He has been a professional systems architect and developer for 12 years. His work focuses on enterprise integration, business intelligence, data warehouses, and workflow-driven portals using SharePoint. Jeff has authored several .NET books, including Expert Service Oriented Architecture in C# (Apress, 2006).