Code Solution for SEO Bugs in BlogEngine

Jun 5, 2013 at 2:15 AM
Hello, I want to share the solution for the SEO bugs detected in this thread: https://blogengine.codeplex.com/discussions/445678

The problem is that multiple URLs can access to the same resource, generating duplicate content for search engines and affecting the SEO of our websites.

You have to modify the file: post.aspx.cs

At line 64 you find this line after an else statement:
this.Post = post;

You only have to add this code:
if (Request.RawUrl != post.RelativeLink.ToString())
{
Response.Clear();
Response.StatusCode = 301;
Response.AppendHeader("location", post.RelativeLink.ToString());
Response.End();
}
I tested the solution and all the URLs redirect to the correct one.
Coordinator
Jun 5, 2013 at 4:31 AM
With this code you won't be able to post any comments, because check doesn't take into account query string and redirects to 301 some legit calls.
You would need at least modify it to remove query string before comparison:
var rawUrl = Request.RawUrl.Replace(Request.QueryString.ToString(), "").Replace("?", "");
if (rawUrl != post.RelativeLink.ToString())
{
    Response.Clear();
    Response.StatusCode = 301;
    Response.AppendHeader("location", post.RelativeLink.ToString());
    Response.End();
}
I checked it in with 2.8.0.5 but it probably needs some more testing to make sure we not breaking anything else.
Jun 5, 2013 at 5:41 AM
I just tested the code but it didn't work. To approve or delete a comment I use the comment panel. The direct links for comments in the posts don't work.
Jun 5, 2013 at 6:27 AM
I just tested this code and it works fine with the links for moderating comments, please check if it's ok:
 if (Request.RawUrl.ToString() != post.RelativeLink.ToString() && (Request.QueryString.Count < 2))
 {                   
     Response.RedirectPermanent(post.RelativeLink.ToString(),true);
  }