
|
Using the Microsoft URL Rewrite Module for IIS 7.0 By John Peterson | |
Rating: 4.6 out of 5 Rate this article
|
email this article to a colleague
suggest an article
|
An Introduction to URL Rewriting
One of the greatest things about dynamic Web sites is the fact that you can build one
page and based on the parameters that are passed to that page it can display any
number of different results. For example, let's say that you're building a Web-based
grocery store. Instead of creating a separate static HTML page for each category
of product you carry, you can simply create a single Category.aspx file
and pass it a parameter indicating what category of products you want to display:
http://www.MyVirtualGroceryStore.com/Products/Category.aspx?Category=Fruit
http://www.MyVirtualGroceryStore.com/Products/Category.aspx?Category=Meat
http://www.MyVirtualGroceryStore.com/Products/Category.aspx?Category=Vegetables
While the resulting Web pages are all generated by the single script, they'll
each display a very different lists of items. This type of power is great
for us as developers, but it's not so good for users and even worser if you're
trying to get the pages indexed by search engines.
While I created the examples above to be as user-friendly as possible,
they're still not all that simple to the average user. These are simpler
and much more intuitive:
http://www.MyVirtualGroceryStore.com/Products/Fruit
http://www.MyVirtualGroceryStore.com/Products/Meat
http://www.MyVirtualGroceryStore.com/Products/Vegetables
So, ever since developers started building dynamic pages and confusing
users and search engines, people have been trying to find a way to get the best
of both worlds. One of the most common solutions is URL rewriting.
URL rewriting is where a user can request a page via the user-friendly URL:
http://www.MyVirtualGroceryStore.com/Products/Fruit
and the Web server figures out to actually use the the script located at:
http://www.MyVirtualGroceryStore.com/Products/Category.aspx?Category=Fruit
to build and return the resulting Web page.
This concept is not a new one. Developers have been doing it for quite some time and
in addition to the numerous third-party URL rewrite products available, ASP.NET
actually includes some built-in support for it via
URL Mapping
and the
HttpContext.RewritePath Method.
The problem is that using them can be relatively complex and each approach comes with its own
drawbacks.
Note: Since this article's focus is on getting the IIS 7.0 Rewrite Module up
and running, I'm not going to do an in-depth discussion of each of the different approaches.
Scott Guthrie has an excellent post which covers the different approaches here:
ScottGu's Blog: Url Rewriting with ASP.NET.
If you're not running IIS 7.0 (or later) or you simply decide that the module discussed in
this article is not appropriate for your project, in addition to Scott's Blog (and the links
you can find there) you may also find the following articles useful:
MSDN: URL Rewriting in ASP.NET
and 15 Seconds: Rewrite.NET -- A URL Rewriting Engine for .NET
Microsoft URL Rewrite Module for IIS 7.0
The first step is to make sure you can use the module. You'll need to make sure you're running IIS 7.0
or later (which means Windows Server 2008 or Vista). You'll also need interactive access to the server
to run the install routine. If you meet the above requirements, you can download the version of
the module that matches your server's platform from the IIS 7.0 Web site:
It's a relatively small download and shouldn't take you very long to get.
The installation is quick and doesn't offer any configuration options.
One the module is installed, if you run "Internet Information Services (IIS) Manager",
go to your Web site, and look in the "Features View", you should now see an icon labeled
"URL Rewrite".
Clicking the icon will bring you to the URL Rewrite configuration area.
To create a rule click on the "Add Rules..." link in the "Actions" pane on the right.
That will bring up the following dialog box from which you can select a rule template.
Selecting the "Blank Rule" template will let you build your own rule by hand, but since
we're building a rule to define user-friendly URLs, let's use the "User friendly URL"
template. The template asks for a sample of the actual URL our application is using so I'll give
it our example from earlier:
http://www.MyVirtualGroceryStore.com/Products/Category.aspx?Category=Fruit.
From that it offers some suggestions for what you might want the user-friendly URL to be.
None of the choices fit exactly, but the third one is close to what I want so I'll choose
that one and simply remove the "Category" part from the "URL pattern" box:
Clicking the "OK" button will add the rule and I never even had to brush up
on my regular expression syntax.
A Quick Look from a Client-Side Browser
Okay so what did we just accomplish? Let me show you. I set up a very simple page that just
writes back out what it receives via the QueryString to illustrate how things work. Before
adding our URL rewriting rule, I can access the page via the real URL:
But, using the user-friendly URL results in a familiar error message:
After adding the rule discussed above, visiting the same user-friendly URL
loads the correct page and the visitor never even knows that the "/Products/Fruit"
folder doesn't exist and that the page was actually generated by the
"Category.aspx" script.
Conclusion
I hope this article has shown you just how easy it is to implement URL rewriting by using the
Microsoft URL Rewrite Module for IIS 7.0. While the module is extremely powerful and
robust, it's also quite easy to install and use. While some of the more advanced features
may take some time to become familiar with, even a novice developer should be able to
get the basics up and running in just a few minutes.
For a more in-depth coverage of the IIS 7.0 URL Rewrite Module, take a look at the excellent
documentation available here:
IIS.NET: Microsoft URL Rewrite Module for IIS 7.0 (x86) Documentation.
|
|
|
|
|
|
|
Other Articles
|
|
|
|
|
Apr 28, 2005 - New Files and Folders in ASP.NET 2.0
|
|
|
With the release of ASP.NET 2.0, Microsoft has greatly increased the power of ASP.NET by introducing a suite of new features and functionalities. As part of this release, ASP.NET 2.0 also comes with a host of new special files and folders that are meant to be used to implement a specific functionality. This article examines these new files and folders in detail and provides examples that demonstrate how to utilize them to create ASP.NET 2.0 applications.
[Read This Article] [Top]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Dec 15, 2004 - A Sneak Peek at ASP.NET 2.0's Administrative Tools
|
|
|
With ASP.NET 2.0, Microsoft has made great strides in increasing developer productivity and has made implementing previously complex solutions relatively easy. Where this version of ASP.NET really shines, however, is in its new administrative tools that allow developers to spend less time managing the configuration of the servers and software and more time developing great code.
[Read This Article] [Top]
|
|
|
|
Nov 17, 2004 - The ASP.NET 2.0 TreeView Control
|
|
|
Thiru Thangarathinam introduces ASP.NET 2.0's new TreeView control which provides a seamless way to consume and display information from hierarchical data sources. The article discusses this new control in depth and explains how to use this feature rich control in your ASP.NET applications.
[Read This Article] [Top]
|
|
|
Mailing List
Want to receive email when the next article is published? Just Click Here to sign up.
|
|