ASP.NET Extensionless URL ReWriting in IIS 7

URL Rewriting is a method by which you can clean up the URL that is displayed to your Web site's users as well as do some SEO (Search Engine Optimization) on your site. So instead of having URLs like http://<server>/products.aspx?categoryid=123, you can create a URL that looks like http://<server>/products/books.aspx. Internally, this gets automatically mapped to the correct URL for you. ASP.NET allows you to do this quite easily. And there are a number of free URL Rewriting components that are available as well. The one that I've used and highly recommend is UrlRewriting.net. (There is another one - also free - called UrlRewriter.net but one that I've had not too great experiences with.)

To get UrlRewriting.net to work, simply copy the provided DLL into the ASP.NET site's /bin folder. Next open up Visual Studio for the site and edit Web.config as follows.

1. Add a <configSections> node to the main <configuration> part of the Web.config as follows:

<configSections>
<section name="urlrewritingnet" restartOnExternalChanges="true"
requirePermission="false"
type="UrlRewritingNet.Configuration.UrlRewriteSection, UrlRewritingNet.UrlRewriter" />
</configSections>

 

2. In <system.web>, add the following section:

<httpModules>
<add name="UrlRewriteModule" type="UrlRewritingNet.Web.UrlRewriteModule, UrlRewritingNet.UrlRewriter" />
</httpModules>

 

3. In <system.webServer>, add the following:

<modules>
<add name="UrlRewriter" type="Intelligencia.UrlRewriter.RewriterHttpModule" />
</modules>

 

4. Create a new section, <urlrewritingnet> in which you'll add the "rules" for the rewriting. For example:

<urlrewritingnet rewriteOnlyVirtualUrls="true"
contextItemsPrefix="QueryString" defaultPage="default.aspx" xmlns="http://www.urlrewriting.net/schemas/config/2006/07" >
<rewrites>
<add name="GetProductCategories"
virtualUrl="^~/Categories/(.*).html"
rewriteUrlParameter="IncludeQueryStringForRewrite"
destinationUrl="~/ViewCategories.aspx?cid=$1"
ignoreCase="true" />
<add name="GetProduct"
virtualUrl="^~/Products/(.*).html"
rewriteUrlParameter="ExcludeFromClientQueryString"
destinationUrl="~/ViewProduct.aspx?pid=$1"
ignoreCase="true" />
</rewrites></urlrewritingnet>

 

The above example rewrites 2 sets of URLs: the first for getting categories (in the form: http://server/Categories/Books.html which redirects to http://server/ViewCategories.aspx?cid=Books) and the second for products (in the form: http://server/Products/au3yuad0494.html to http://server/ViewProduct.aspx?pid=au3yuad0494).

Now one of the biggest problems with this is that you cannot have extension-less rewriting in IIS. That is, a URL like http://server/categories/books is invalid as the final parameter doesn't have an extension. This is a bit of a problem and takes quite a bit of doing (writing an ISAPI filter) to get it to work on IIS 5/6.

The great thing is that IIS 7 (in Windows Vista and the upcoming Windows Server 2008) supports extensionless rewriting out of the box. All that you need to do is one small change in the Web.config shown above. In step 3, use the following instead:

 

<modules runAllManagedModulesForAllRequests="true">
<add name="UrlRewriter" type="Intelligencia.UrlRewriter.RewriterHttpModule" /></modules>

 

The runAllManagedModulesForAllRequests="true" property ensures that all calls made to this site is processed through the ASP.NET 2.0 engine - including calls that do not have an extension. You can now go ahead and write your rules like this:

 

<urlrewritingnet rewriteOnlyVirtualUrls="true"
contextItemsPrefix="QueryString" defaultPage="default.aspx" xmlns="http://www.urlrewriting.net/schemas/config/2006/07" >
<rewrites>
<add name="GetProductCategories"
virtualUrl="^~/Categories/(.*)"
rewriteUrlParameter="IncludeQueryStringForRewrite"
destinationUrl="~/ViewCategories.aspx?cid=$1"
ignoreCase="true" />
<add name="GetProduct"
virtualUrl="^~/Products/(.*)"
rewriteUrlParameter="ExcludeFromClientQueryString"
destinationUrl="~/ViewProduct.aspx?pid=$1"
ignoreCase="true" />
</rewrites></urlrewritingnet>

 

In this case, there are no ".html" extensions for the URL and you can have valid ones like http://server/Categories/Books or http://server/Products/sadk442. Remember, this makes the structure of your site seem much more friendly to your user as well as get an additional benefit of being search engine friendly as well.


Tags: , ,
Categories:

15 Comments
Actions: E-mail | Permalink | Comment RSSRSS comment feed

Comments

thanks for the code, i just cant' wait to try out Laughing

December 26. 2008 18:13 | Busby SEO Test United States |

After setting runAllManagedModulesForAllRequests="true"

cause a problem all requests will be entertained by asp.net, so for each request iis will forward request to asp.net worker process that cause over load on asp.net worker process

January 6. 2009 17:33 | adeel hussain Bangladesh |

i have just tried your code, it works well. thanks

January 9. 2009 06:10 | belajar seo United States |

interesting post, thanks for sharing.

March 10. 2009 01:49 | phreakaholic United States |


I’m really impressed with your article, such great information you mentioned here, thanks for your sharing and waiting to see your future posts.

March 13. 2009 12:57 | funny sayingq quotes United States |


Thank you for another great article. Where else could anyone get that kind of information in such a perfect way of presentation.

March 20. 2009 17:17 | food quotes United States |

Very entertaining Site. I'd have to say nice article if i do say so myself. nice site. I hope you don't mind I've put you in my rss feed now. I'm excited and look forward to your Updates.

April 3. 2009 09:31 | Thesis Writing United States |

Send Gifts to Srilanka, Flowers to Srilanka, Cakes and Chocolates to Sri Lanka and Colombo.

May 7. 2009 23:42 | lanka florist Sri Lanka |

Wow, I never knew that ASP.NET Extensionless URL ReWriting in IIS 7. That’s pretty interesting...

June 12. 2009 16:10 | Internet Marketing Company United States |

Basically you can add an application mapping to '.*' in the application config and all requests will go to the ASP.NET engine."Can you elaborate more on this? I can't seem to find where the configuration part to change.

June 18. 2009 15:45 | rules to gambling India |

hello, this is my first time i visit here. I found so many interesting in your blog especially its discussion. keep up the good work.

June 20. 2009 05:52 | Sulumits Retsambew United States |

I like it and I think you make a good code. Thanks for taking the time to share this with us.
I am always searching for informative information like this.

June 24. 2009 21:31 | tukang nggame United States |

After setting runAllManagedModulesForAllRequests="true" cause a problem all requests will be entertained by asp.net, so for each request iis will forward request to asp.net worker process that cause over load on asp.net worker process..

July 5. 2009 14:11 | club penguin cheats United States |

Send Gifts to Srilanka, Flowers to Srilanka, Cakes and Chocolates to Sri Lanka and Colombo.

July 9. 2009 07:54 | lankaflorist Sri Lanka |

great dude

July 13. 2009 00:39 | rusli Zainal Sang Visioner United States |

Comments are closed