Date in Post Url is Not Applied Accurately


In the UrlRewrite module, the RewritePost() method is not matching the date in a post's url correctly to the date of a post. Here are the conditions:
  1. Two blog posts have the same slug.
  2. The blog posts are posted on different days.
  3. The 'Add date to post links' option is selected.
If you try to access the older post, BE currently retrieves the newer post (this is incorrect). The relevant code in RewritePost() looks like:

if (date != DateTime.MinValue && (p.DateCreated.Year != date.Year || p.DateCreated.Month != date.Month))
if (p.DateCreated.Day != 1 && p.DateCreated.Day != date.Day)
    return false;

I don't understand what the "p.DateCreated.Day != 1" part is for, but the code below works correctly. Note, I omitted the "p.DateCreated.Day != 1" part just because it doesn't seem necessary.

if (date != DateTime.MinValue &&
(p.DateCreated.Year != date.Year ||
p.DateCreated.Month != date.Month ||
p.DateCreated.Day != date.Day))
return false;

This fixes the problem described above.

There's still a separate, but related problem however, if the 'Add date to post links' option is not selected. In this case, if you try to access the older post, BE still retrieves the newer post. Even if you try to get to the older post via the Permalink, BE still shows the newer post. The reason for this is because when a post is retrieved by guid, in post.aspx.cs, a 301 redirect is done with the new location being the RelativeLink. When this is done, the RewritePost() method in UrlRewrite can't distinguish between the older post and the newer post based on the RelativeLink when there's no date in the url.
Closed Oct 23, 2009 at 8:50 AM by
Fixed in


BenAmada wrote Feb 10, 2009 at 7:17 PM

Here's one possible solution for this last scenario I described so a post would always be retrievable by it's permalink.

The other change I wrote up in this issue for the RewritePost() method in UrlRewrite would still want to be made.

The change here is in Post.aspx.cs. I added the IF statement before the 301 redirect to make sure there isn't another post with the same RelativeLink as this post. If there is another post, then don't do a 301 redirect since UrlRewrite won't be able to distinguish between the posts based on the RelativeLink.

===== Post.aspx.cs Code =====
if (Request.RawUrl.Contains("?id=") && Request.QueryString["id"].Length == 36)
Guid id = new Guid(Request.QueryString["id"]);
Post post = Post.GetPost(id);
if (post != null)
    // If there's more than one post that has the same RelativeLink
    // this post has then don't do a 301 redirect.

    if (Post.Posts.FindAll(delegate(Post p)
        { return p.RelativeLink.Equals(post.RelativeLink); }
    ).Count < 2)
        Response.StatusCode = 301;
        Response.AppendHeader("location", post.RelativeLink.ToString());