Lately, topics like search engine optimization and writing user-friendly URLs are to find everywhere on the Internet. Particularly, rewritten URLs make it a lot easier to embed topic-specific or product-specific keywords into the URLs of the pages on your website which increases accessibility for both your audience and for various web crawlers. This is accomplished by moving away from typical query string arguments like for e.g.:
http://nedzadsmajic.info/?p=11&preview=true&a1=more&a2=argu&a3=ments&a4=0&a5=1&a6=2&a7=etc to a more readable form like: http://nedzadsmajic.info/2008/06/04/url-rewriting-for-aspnet.
After evaluating several URL rewriting engines, the component of my choice was the open source component called “UrlRewritingNet.UrlRewrite”, because it was really easy to implement.
Functions of UrlRewritingNet.UrlRewrite are:
And there are some drawbacks too:
To use the “UrlRewritingNet.UrlRewrite” component, follow these steps:
Configuration schema changes
Add following entries to your web.config file:
<configSections> <section name="urlrewritingnet" restartOnExternalChanges="true" requirePermission ="false" type="UrlRewritingNet.Configuration.UrlRewriteSection, UrlRewritingNet.UrlRewriter" /> </configSections>
To handle all incoming requests with UrlRewritingNet you have to register the component as Http Module in the
<httpModules> <add name="UrlRewriteModule" type="UrlRewritingNet.Web.UrlRewriteModule, UrlRewritingNet.UrlRewriter" /> </httpModules>
Finally, you have to add the rewriting rules. It is recommended to write them as specific as possible, for example not “^~/(.*)/(.*).aspx”:
<urlrewritingnet rewriteOnlyVirtualUrls="true" contextItemsPrefix="QueryString" defaultPage = "Default.aspx" defaultProvider="RegEx" xmlns="http://www.urlrewriting.net/schemas/config/2006/07" > <!--URL Rewriting Section--> <rewrites> <!--products--> <add name="ProductsPageRule" virtualUrl="^~/product/([a-zA-Z][\w/_-]{1,149})_(.+).aspx" rewriteUrlParameter="ExcludeFromClientQueryString" destinationUrl="~/Products.aspx?id=$2&ot=S&pageName=O" ignoreCase="true" /> <!--authors--> <add name="AuthorsPageRule" virtualUrl="^~/author/([a-zA-Z][\w/-]{1,149})_(.+).aspx" rewriteUrlParameter="ExcludeFromClientQueryString" destinationUrl="~/Authors.aspx?id=$2&ot=N&pageName=O" ignoreCase="true" /> <!--categories--> <add name="CategoriesPageRule" virtualUrl="^~/category/([a-zA-Z][\w/-]{1,149})_(.+).aspx" rewriteUrlParameter="ExcludeFromClientQueryString" destinationUrl="~/Categories.aspx?id=$2&ot=R&pageName=O" ignoreCase="true" /> </rewrites> </urlrewritingnet>
Explanation of the terms:
virtualUrl – A regular expression which is used for replacing the current URL with destinationUrl.
destinationUrl – A regular expression term describing the target page (physical file).
Tip #1:
If you’re going to use more comprehensive rules, it’s a good practice to move them to a separate configuration file.
Finally, rewrite your respective URLs so they conform to the rewriting rules.
4 Responses
vinay
June 16th, 2008 at 07:01
1Hi,
I am using UrlRewritingNet.UrlRewrite to rewrite my URLs and everything seems to work fine, the only problem being when i login/logout from a page the querystring is added at the end of rewritten URL.
For Eg. If i am in a page say http://www.dummy.com/Post/28/test.aspx and if i login/logout from this page the url becomes http://www.dummy.com/Post/28/test.aspx?Id=28.
I thought this was a postback issue, but all other postback are handled properly. Its only with the login i am facing this problem.
How can i solve this
Nedzad
June 16th, 2008 at 18:42
2Hi Vinay,
thanks for dropping by.
I don’t think that is the issue with URL Rewriting engine, the problem might lie in the behavior of FormsAuthentication class (I assume you’re using the “FormsAuthentication.SignOut()” method), which adds ReturnURL parameters at the end of your login page’s querystring. You can use either Response.Redirect to redirect to your login page and do there “logout” stuff or you can use cookies for your forms authentication and that would prevent “infinite” redirects.
Just add cookieless=”UseCookies” to your “Forms” section in the web.config.
Good luck!
vinay
June 17th, 2008 at 07:23
3Hi Nedzad,
Thanks a lot, I will use the solution you have given and see if it solves my problem.
Mersed
June 25th, 2009 at 10:52
4Hey pal, this is great post, thank you.
RSS feed for comments on this post · TrackBack URI
Leave a reply
Categories
Archives
Links
Meta
Calendar
Recent Entries
Recent Comments
Most Commented
.NET Framework – Aggregated is proudly powered by WordPress - BloggingPro theme by: Design Disease