REST Friendly URLs
Lest say you have an articles site, which have:
- Category: credit-card
- Sub Category: student
- Article Id: students-heed-parents-on-credit-card-advice
The following two samples describes the difference between friendly URLs and Dirty URLs:
Why Does URL Rewriting Matter?
There are some reasons why me matter about rewriting URLs to get a clean version of them:
- Bookmaking – The URLs are part of your API in REST world, and such, client may bookmarks them or have some client programs that works with your API links. Your goal is to stay with the same URL no matter if you changing tomorrow your technology.
- Usability – Friendly URLs are easy to remember and provides cues to the user about the resource itself.
- Hide implementation details – The main reason is abstraction. The user doesn't require to know how I implemented it.
- Security – The query string which follow the question mark (?) is often modified by hackers.
- Logical URLs – If you have a site with 400,000 pages, it is not easy to maintain all of them, for example, you need to add some script for all pages…, using logical you can use only few physical pages to serve all the pages Logical URLs means that any URL in your site represents resource which is not physical but logical.
- Search Engine Optimization (SEO) – When search engine gets URL which imply that the underlying site is dynamic, and use query parameters, it treats to him different than he gets a friendly URL.
URL Rewriting with IIS 7.0
Intercepting request with IIS 7.0 is very easy:
- Implement HttpModule
- Configure it in Web.Config
The following sample is show how to remove '.svc' extension from WCF service hosted in IIS:
public void Init(HttpApplication app)
app.BeginRequest += delegate
HttpContext ctx = HttpContext.Current;
String path = ctx.Request.AppRelativeCurrentExtensionFilePath;
ctx.RewritePath(path + “.svc”, …);
URL Rewriting with UrlRewriter.NET
- Download from http://urlrewriter.net/
- Register it in Web.Config (configSections, httpModules, modules):
<add name="UrlRewriter" type="Intelligencia.UrlRewriter.RewriterHttpModule, Intelligencia.UrlRewriter"/>
<add name="UrlRewriter" type="Intelligencia.UrlRewriter.RewriterHttpModule"/>
- Define your mapping:
- The source code for Article.aspx.cs is show in the following sample:
protected void Page_Load(object sender, EventArgs e)
ASP.article_aspx page = sender as ASP.article_aspx;
if (page == null)
String categoryId = page.ApplicationInstance.Request.Params["categoryId"];
String subCategoryId = page.ApplicationInstance.Request.Params["subCategoryId"];
String articleId = page.ApplicationInstance.Request.Params["articleId"];
Friendly URLs are simple to implements, and have a big rewards on this few additional lines of code. I show here two methods to use it, there are few more (ISAPI filters on IIS 6.0, HttpContext.RewritePath() in ASP.NET), but I wanted to concentrate on the better approaches.
With Friendly URLs your site links (and API) are clean, easy to remember, SEO friendly, and has abstraction level.