Tuesday, March 27, 2012

routing within longtail links is the stuff of .htaccess but not ASP.NET MVC unfortunately

Have you ever wished you could open the Global.asax.cs file in an ASP.NET MVC application and replace this...

routes.MapRoute(
   "Default",
   "{controller}/{action}/{id}",
   new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

 
 

With this...

routes.MapRoute(
   "Default",
   "{controller}-{action}-{id}",
   new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

 
 

Your answer is: "No, why would I?" isn't it? I have a hunch that you don't see any reason to use hyphens in lieu of slashes as I suspect few do. I base my suspicion on the fact that four versions into ASP.NET MVC this sort of customization still isn't supported. Consider this URL however:

http://www.example.com/Sunglasses-Inventory-Rayban.html

 
 

Imagine running this URL with the second of the routing rules above. We would navigate to the Inventory action within the Sunglasses controller and pass "Rayban.html" as the id. The ".html" part of the id could be truncated away later. This sort of implementation would empower longtail (or long tail) SEO links. A more sophisticated set of routing rules would allow for even more content specific stuff to be concatenated with hyphens. Yay! This would work really well for SEO!

...and yet, this is not the way ASP.NET MVC works. What are the alternatives? How could we make this work?

ISAPI Rewrite allows one to run the .htaccess configuration files one associates with Apache on IIS. An .htaccess file will allow one to rewrite an inbound request of:

http://www.example.com/foo-bar-baz.html

 
 

To:

http://www.example.com/foo/bar/baz/

 
 

I've seen this stuff in action before working at framesdirect.com. http://www.blogstorm.co.uk/htaccess-mod_rewrite-ultimate-guide/ has the best bad example I've found in Googling tonight. It shows www.yoursite.com/product/123 rewritten to www.yoursite.com/product.php?id=123 with the following rule. Perhaps you can imagine how this rule could be written the other way around.

RewriteEngine on
RewriteRule ^product/([^/\.]+)/?$ product.php?id=$1 [L]

No comments:

Post a Comment