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:
- 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" }); -
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" }); -
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" }); -
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:
- An invalid controller or action is specified
- 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
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