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:
url rewriting,
asp.net,
iis7
Categories: