Saturday, September 24, 2011

SEO Friendly URL using URL Routing in ASP.NET

Many SEO (Search Engine Optimization) professionals say that dirty URLs are not appealing to web search engines. ASP.NET web pages are basically dynamic web pages and so tend to be dirty URLs. It is common that ASP.NET .aspx page has some parameters in its URL. For example, a sample of dirty URL is like http://x.com/shop.aspx?Category=Car&Maker=Ford. Search engines tend to have difficult time to figure query string parameters. SEO friendly URL is a.k.a clean URL which has folder-like structure. The dirty URL above can be rewritten to http://x.com/shop/car/ford. And from userability point of view, it's much easier to remember as well.

By and large, there are two ways of making website more SEO friendly. One is URL Rewriting and the other is URL Routing.

URL Rewriting
When a client issues a URL to Web server, the URL-rewriting ISAPI DLL analyzes the requested URL and changes it to other URL before it sends the request to web page. Most IIS servers (v5, v6, v7) supports it.

URL Routing
ASP.NET URL routing does not actually change incoming URL and dispatches a URL request to a specific handler based on the requested URL path. Works on latest ASP.NET version (3.5, 4.0).

So assuming we're using ASP.NET 4.0, let's take a look at how to make ASP.NET webpage more SEO friendly by using URL routing scheme.

1) Define URL Routing tables in Global.asax. In Global.asax;
void Application_Start(object sender, EventArgs e)
{
    this.RegisterRoutes(RouteTable.Routes);
}

void RegisterRoutes(RouteCollection routes)
{            
    //If you use ASP.NET AJAX
    routes.Ignore("{resource}.axd/{*pathInfo}");

    // Define Routing Tables
    routes.MapPageRoute("item", "shop/{item}", "~/shop.aspx");
}

If you use ASP.NET AJAX, you might get an error:
'ASP.NET Ajax client-side framework failed to load.'
This can be avoided by adding routes.Ignore statement above.

MapPageRoute() method has many overrodes but here first parameter is route map name; you need to put any unique mapping name. 2nd parameter is URL input routing pattern. With this pattern, the incoming URL is like http://x.com/shop/iPhone. In this case, the string 'iPhone' corresponds to {item}. 3rd parameter is physical file that will process the URL. So shop.aspx will take parameter {item} (which is iPhone) and process the incoming URL request.

2) Process URL request in designated .ASPX page.
In Page_Load() method of shop.aspx.cs file, {item} data can be retrieved by looking up Page.RouteData.Values collection.
protected void Page_Load(object sender, EventArgs e)
{
  string item = Page.RouteData.Values["item"] as string;                
  if (!string.IsNullOrEmpty(item))
  {   
      // set item data here
      return;
    }
  }
  Response.Redirect("/Default.aspx");
}

So URL routing is quite simple, but it's powerful in terms of SEO.
One thing worthy adding is that Routing tables are looked up sequentially, so one can add special routing case before generic routing pattern.

No comments:

Post a Comment