User Friendly URL - rule template
by IIS Team
Rule templates are used to provide a simple way of creating one or more rewrite rules for a certain scenario. URL rewriter module includes several rule templates for some common usage scenarios. In addition to that URL rewrite module UI provides a framework for plugging in custom rule templates. This walkthrough will guide you through how to use "User Friendly URL" rule template that is included with URL rewrite module.
This walkthrough requires the following prerequisites:
- IIS 7.0 or above with ASP.NET role service enabled;
- URL rewrite module 2.0 release installed.
Setting up a test web page
We will be using a simple test asp.net page to verify that the rules created by the template work correctly. The test page simply reads the web server variables and outputs their values in browser.
Copy the following ASP.NET code and put it in the
%SystemDrive%\inetpub\wwwroot\ folder in a file called article.aspx:
<%@ Page Language="C#" %> <!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>URL Rewrite Module Test Page</h1> <table> <tr> <th>Server Variable</th> <th>Value</th> </tr> <tr> <td>Original URL: </td> <td><%= Request.ServerVariables["HTTP_X_ORIGINAL_URL"] %></td> </tr> <tr> <td>Final URL: </td> <td><%= Request.ServerVariables["SCRIPT_NAME"] %>?<%= Request.ServerVariables["QUERY_STRING"] %></td> </tr> <tr> <td>Public Link: </td> <td><a href="<%= Request.ServerVariables["SCRIPT_NAME"] %>?<%= Request.ServerVariables["QUERY_STRING"] %>">Link URL</a></td> </tr> </table> </body> </html>
Using rule template to generate rewrite rules
The "User Friendly URL" rule template can be used to generate rewrite, redirect and outbound rules that make URLs for your dynamic web application more user and search engine friendly. Typically, dynamic web pages take into account query string parameters when generating an output HTML. The URLs with query strings (e.g.
http://contoso.com/articles.aspx?year=2008&month=11) are not as easy for humans to use and communicate as simple hierarchy based URLs (e.g.
http://contolso.com/articles/2008/11). In addition some search engine crawlers may ignore the query string when indexing the web site pages. The rule template helps you generate rewrite rule that transform the hierarchy based URLs to URLs with query strings. The templace can also, optionally, generate a redirect rule that can be used to redirect web clients form URLs with query strings to clean URLs. Finally, it is possible to create an outbound rewrite rule that replaces all the occurances of URLs with query strings in the HTML responce with their hierarch based URL equivalents.
To use the template follow these steps:
- Go to IIS Manager
- Select "Default Web Site"
- In the Feature View click "URL Rewrite"
- In the "Actions" pane on right hand side click on "Add rules…" and then select "User Friendly URL" template:
- In the "Add rules to enable user friendly URLs" dialog enter an example of an URL with query string parameters:
http://localhost/article.aspx?id=123&title=some-titleand then expand the drop down list with suggested options for how that example URL can be transformed into a URL without query string.
- Choose the second option:
http://localhost/article/123/some-title. Notice that the URL pattern and Substitution URL have been updated accordingly. These will be used in the rewrite rule that will be created by the rule template. Check the "Create corresponding redirect rule" to create a redirect rule that will be used, when web clients used internal URL to request a web page. Those clients will be redirected to a corresponding public URL.
Also, check the "Create corresponding outbound rewrite rule" to create an outbound rule that will replace all instances of internal URLs in the response HTML with their public equivalents.
- Click "OK" so that the rewrite, redirect and outbound rules will be generated:
Testing the rule
To test the generated rules, open a Web browser and request the following URL:
You should see that the rewrite rule on web server has changed the original URL to article.aspx and it has passed "234" and "some-title" as values for query string parameters.
In the web broser move the mouse over othe "Link URL" text (or use menu "Page" -> "View Source..."). Notice that even though the URL for the link was originally in a format that used query string parameters, it has been replaced by URL Rewrite Module to use hierarchy based URL format.
Also, if you request
http://localhost/article.aspx?id=432&title=some-other-title you will see that the browser gets redirected to
In this walkthrough you have learned how to generate rewrite rules by using "User Friendly URL" rule template included in URL rewrite module. This rule template can be used as a starting point for designing rewrite rules for enabling user friendly and search engine friendly URLs for you existing web applications.