Legacy & Not-Found Links

By
Dave
Project
Published
10 Jul 2010 17:06
Last Modified
3 Jan 2011 01:32

I needed to deal with existing links to my previous blog engine, as well as invalid requests to the new engine.

Legacy Links

As I mentioned previously, my new blog engine has opted for a different URL structure to my old engine. In order to support existing links to my old blog posts, I added some legacy routes to my routing table, as follows:

  1. Category links of the format http://{domain}/{directory}/category/{name}.aspx are mapped to my existing controller with the following route:
    routes.MapRoute(
        "MyRoute",
        "category/{name}.aspx",
        new { controller = "MyController", action = "MyAction" });
    
  2. Post links of the format http://{domain}/{directory}/post/{name}.aspx are mapped to my existing controller with the following route:
    routes.MapRoute(
        "MyRoute",
        "post/{name}.aspx",
        new { controller = "MyController", action = "MyAction" });
    
  3. Month links of the format http://{domain}/{directory}/{year}/{month}/default.aspx are mapped to my existing controller with the following route:
    routes.MapRoute(
        "MyRoute",
        "{year}/{month}/default.aspx",
        new { controller = "MyController", action = "MyAction" });
    
  4. The syndication link http://{domain}/{directory}/syndication.axd?format={format} is mapped to my existing controller with the following route:
    routes.MapRoute(
        "MyRoute",
        "syndication.axd",
        new { controller = "MyController", action = "MyAction" });
    

The nice thing about this approach is that I can support these legacy links without any additional code, just these additions to my routing table.

Not-Found Links

Requests to the new blog engine might not return anything for a couple of reasons:

  1. An invalid controller or action is specified
  2. A invalid parameter is passed to a controller action

For invalid controller requests, a catch-all route is a good starting point, such as the following:

routes.MapRoute(
    "Error",
    "{*url}",
    new { controller = "Error", action = "Http404" });

This ensures that requests of an unrecognised pattern will be passed to an error controller, and an appropriate view used to render an error.

For invalid parameters, additional code is required in the relevant controllers to redirect to a "Not Found" view, for example when a requested post is not found.

Comments

By
Steve
12 Jul 2010 09:24
Wouldn't it be better to use a 302 permanent redirect for old urls? This way google will start using your new urls and stop showing the old ones in the index.
By
Dave
12 Jul 2010 22:48
Hi Steve, thanks for the suggestion. The routing table approach ensured that existing links resolved correctly for users, with no code-changes, but didn't deal with SEO. For the latter, you are correct that I should be issuing appropriate HTTP response codes.

Issuing an HTTP 302 (Found) response results in an extra HTTP round trip when accessing old URLs. Search engines typically will not follow across multiple redirection hops, which means using an HTTP 302 can negatively impact page-ranking. ASP.NET 4 introduced a permanent redirection helper to redirect using an HTTP 301 (Moved Permanently) response. This will cause search engines and other user agents that recognize permanent redirects to store and use the new URL, enablling content to be indexed and search engine page-ranking to improve.

I've just updated to the site to send HTTP 301 return codes. I've got some further work to do on SEO, and will add a post on this subject when this is done.

Add Comment

*
*
*
Captcha
*
*Required