Redirecting to posts in BlogEngine

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.

Comments

2/21/2010 3:52:25 PM #

Jim

If your old site has posts in aspx pages, or if you can configure IIS to process html pages, then there's an extension for BlogEngine that looks really good: www.dexign.net/.../...tension-For-BlogEngine.aspx.

Jim United States

7/29/2010 2:41:47 AM #

replicapot

I noticed your weblbog through a AOL search, How did you get your website to show up in a Internet search?

Took me time to read all the comments, but I really enjoyed the article. It proved to be Very helpful to me and I am sure to all the commenters here It's always nice when you can not only be informed, but also entertained I'm sure you had fun writing this article.

replicapot India

Add comment


(Shows Gravatar icon; will not be displayed)

  Country flag
Click to change captcha
biuquote
  • Comment
  • Preview
Loading