Friendly URLs in ASP.NET using URLRewriter.NET
I was having a chat the other day with Danish Peter (yes, another one) about URL rewrites in ASP.NET. In a previous post I wrote about how to use friendly URLs in the Zend framework, in this post, which is based on our discussion, I will discuss how to do this in ASP.NET.
Friendly URLs are achieved by a technique called URL-rewrite. A friendly URL is a URL that is easy to read and understand. Let me give you an example (from a site my girlfriend loves) of what is NOT a friendly URL:
http://www.bebe.com/bebe-Jersey-Knit-Apron-Dress/dp/B001UX1NBQ?ie=UTF8&asinSearchPageIndex=13&pf_rd_r=1C6TS27BZZ1CAD6WY81S&navAsinList=B001UNM3XS%2CB001OPYKKQ%2CB001UNNQ52%2CB001RPD88I%
2CB001R4NHHQ%2CB0026BM39W%2CB001QOU6TY%2CB001UX1NNY%2CB001UAA4WI
%2CB0024QP6KM%2CB0026RRN4G%2CB0027DCI6W%2CB001UX3QL6%2CB001UX1NBQ
%2CB001VROPT8%2CB00265NBKS%2CB001RPM80C%2CB001UBWNTO%2CB001RPIC1G
%2CB001PA2NMW&node=675941011&pf_rd_s=search-results&field_browse=675941011&searchSize=20&navAsinListIndex=0&pf_rd_t=101&field_availability=0&id=bebe%20Jersey%20Knit%20Apron%20Dress&searchBinNameList=null&store=core&pf_rd_p=476815091&ref=search_results_14&searchNodeID=675941011&pf_rd_i=675941011&field_launch-date=-1y&searchRank=-custom-rank&searchPage=1&pf_rd_m=A2FMOXN01TSNYY
This might be an extreme example, but there are plenty of sites which are as bad. If the above URL was transformed into a friendly URL it might look something like:
http://www.bebe.com/Apparel/Dresses/Jersey-Knit-Apron-Dress.html
The second address is easier to read. URL rewrite techniques have been around for a long time and the first time I used it was back in the early Apache 1.3 days. Today good sites use friendly URLs and if you have not adopted it yet, you need to start using it.
There are several benefits from using a friendly url scheme, the most prominent are:
- 1. The clear structure makes it easier for humans to read and understand the addres
- 2. It is easier for search engines to understand what the address is and crawler the site. URL structure that goes more than three directories deep is not always read by a spider.
- 3. If search engines understand the URL, it is also easier for them to help people finding what they want, giving better search results. Hence using keywords and proper titles makes life easier.
What options are there for ASP.NET?
There is no built in support for having friendly URLs in ASP.NET, but you could spend your time writing your own URL-rewrite module. Writing your own module is basically spending time reinventing the wheel. If you have the time, go ahead, but if not there are already several good ones out there (free and open source). One that I have found very useful is the URLRewriter.NET (http://urlrewriter.net/). URLRewriter.NET provides similar rewrite capabilities as the mod_rewrite does for Apache.
Using URLRewriter.NET
It is possible to use the URLRewriter.NET module without making any changes to the IIS configuration, this will however limit the functionallity the URLRewriter.NET rewriting capabilities. To use its full power, IIS should be configured to map all requests to the ASP.NET runtime (how to do this is covered in the manual). We will only be touching the surface of URLRewriter.NET so we will not have to do any modifications to the IIS configuration.
To add URLRewriter.NET to your project start out by opening Visual Studio or Visual Web Developer. Open your project and then right click the project in the solution explorer and click on Add Reference, go browse and find the folder you downloaded and unzipped. You will find the binary (.dll) file in “Loation”\urlrewriternet20rc1b6\UrlRewriterV2\bin\Release
.
Before we can use the module we need to make some changes to the web.config. Add the following in the configSections:
This will allow the URL writer to read the rewrite rules we will define later. Now we need to add this module to the httpModules section:
By adding the rewriter as a httpModule we allow it to intercept web requests and route the requests to the actual aspx file serving the page. Now we need to define the rewrite rules:
The rewrite rules are defined using regular expressions. Regular expressions are very powerful and is something every programmer should know. The syntax might be hard to remember but there are several great tools to help build advanced regular expressions, for example the RegexDesigner.NET (http://www.sellsbrothers.com/tools/#regexd) that Chris Sells has created.
The above rewrite rules are simple. The first rewrite rule will rewrite URLs like
sourcecodebean.com/2009/04/01/1.shtml
to
sourcecodebean.com/Default.aspx?Year=2009&Month=04&Date=01&Id=1
And the second rewrite rule will match requests starting with ~/Article/ so it will rewrite URLs like
sourcecodebean.com/Article/1.shtml
to
sourcecodebean.com/Articles.aspx?ID=1
Finishing up
By now you hopefully have a functioning site that uses friendly URLs. One of the common problems you might run into is broken links. To avoid this you should make sure that you reference all your pages from the root level (~) and that all your links has the runat=server tag.
Thanks for your input Peter! You can check out his Danish Peter blog over at Magic Mouse.
Hi Peter,
I got my MSSQL database running on the web server of the hosting company. I tried to implement what you said on the database in my laptop – it worked!
However, the feature does not work on the actual website even after I uploaded the urlrewriter.dll and another xml file into the bin folder of the hosting server. I am suspecting that it might be due to the fact that I had “add reference” to the urlrewriter.dll in my local machine, but not the one at the hosting server. Is there anyway to “add reference” for the database hosted by hosting company’s server? Thanks.
Have you modified your web.config on your hosting web-server? You need to add references to URLRewriter as described in the article. Also, I assume that locally you are using the WebDev server that is built into Visual Studio? This webserver behaves slightly different from IIS, in WebDev all requests gets routed to the ASP.NET runtime, while in IIS this is not the case. I think you can find instructions for how to configure this for IIS 6 in the URLRewriter documentation.
hi,
i have a url like
“/travelguide_details.aspx?CountryId=104&RegionId=155&SubRegionId=566”
and this should be redirected to the url like
/stratford-upon-avon-coach-holidays.aspx
can you please help me how to write the rule for this type of links and the id’s and destination region name(stratford-upon-avon) are dynamic.
In order to create that rewrite you need to create some kind of mapping between the name “stratford-upon-avon-coach-holiday” and CountryId=104, RegionId=155 and SubRegionId=566, which will be a bit more complicated. An easier solution would be to create an URL like:
/104-155-566/stratford-upon-avon-coach-holidays.aspx
Not as nice as the url you suggested, but the url will contain the information you need to do the rewrite. With this URL you can create a rule like:
I hope this helps!
hi,
i wrote this
for this output http:~/cheapflightsto-accra-7-H.aspx
its working fine localy in Asp.net and in IIS as well
but when i upload, it gives error i.e.
HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.
Requested URL: /cheapflightsto-accra-7-H.aspx
Hi Sohaib,
It seems like the rewrite module isn’t loaded by IIS. Have you uploaded the UrlRewriter dlls and made the changes to web.config on the server? Are you using the same version of IIS on your local dev machine as on the server? If your server is running IIS7 the web.config has changed and you cannot use the example I pasted in this post.
/Peter
🙂
It’s usefull but
could U give everyone’s an example with a project source code.
Thanks
HIIIIIII
How Are You
How this Code Works on IIS 7
AND Visual Studio 2008
Target FrameWork 2.0
Because It Gave Me Error
Resource Not Found
and Thank You In Advance
hi, where do you place the rewriter tags??
thanks