by Jim
Feb 15, 2010 9:38 PM
Since I’ve moved my blog from Blogger to BlogEngine, the URLs have of course changed. The old posts were .html files, so my asp.net application doesn’t process them, the and resulting 404 error falls to Brinkster (my host) to handle.
I can configure my Brinkster account to load BlogEngine’s Error404.aspx page in these cases, and the following code will successfully redirect those requests to the new BlogEngine post.
Blogger and BlogEngine both follow the convention of naming a post with the title, replacing spaces with commas.
// Jim: check for brinkster 404 redirect query string
// The query string is not name=value, it's this:
// 404;http://www.jimandkatrin.com:80/CodeBlog/2009/12/some-thoughts-on-code-reviews.html
string reg = @"^404;http://www.jimandkatrin.com:80/CodeBlog/[0-9]{4}/[0-9]{2}/(.*)\.html$";
string qs = Server.UrlDecode(Request.QueryString.ToString()); // raw query string
if (!string.IsNullOrEmpty(qs) &&
System.Text.RegularExpressions.Regex.IsMatch(qs, reg,
System.Text.RegularExpressions.RegexOptions.IgnoreCase))
{
// Get the match, and construct a new URL out of it, in the BlogEngine format:
System.Text.RegularExpressions.Match match =
System.Text.RegularExpressions.Regex.Match(qs, reg);
string url = string.Format("~/post/{0}.aspx", match.Groups[1]);
Response.Redirect(url);
Response.End();
return;
}
// Jim: Set response code so that redirects from Brinkster 404 will actually
// return the response code 404
Response.StatusCode = 404;
Response.Status = "404 Not Found";
The status code at the end causes the page to actually return a 404 status code, rather than a 200 OK, when Brinkster redirects to this page.