This project is read-only.

Redirect when changing the "slug"?

Topics: Controls
Feb 25, 2009 at 3:39 PM
Hi Folks:

Is their a configuration setting in that automatically redirects when someone changes the slug of a post (causing requests to the old slug to give a not found error)?

Or should I just do this in IIS with perm. redirects?

Any thoughts?


Feb 25, 2009 at 6:21 PM
I don't remember seeing such a setting.  Are you looking to redirect the person to the correct page, or do you want to show a 'page not found' error?

With IIS, you could display the error404.aspx page, as you mentioned. 

You could also modify the BE core code to display the error404.aspx page for invalid slugs.  To do this, in the UrlRewrite.cs file in the BE core, there's a RewritePost() method.  In there, you would want to add the bolded part below.

if (post != null)
   .... some code ....
    context.Response.Redirect(Utils.RelativeWebRoot + "error404.aspx");

Then in error404.aspx.cs, I would recommend setting the HTTP status code to 404.

protected void Page_Load(object sender, EventArgs e)
  Response.StatusCode = 404;  
  ... existing stuff ...

But for just showing the error404.aspx page, I'm thinking you're best off just doing this within IIS (rather than making the changes above).
Mar 2, 2009 at 3:44 PM
I had a client wanting to change existing pages and have any request automatically redirect to the new pages ... I can do this in IIS, but just wanted to see if there was an automated method of doing this.
Mar 2, 2009 at 7:20 PM
There's nothing built-in for doing these types of redirects, but I just put something together you can use which is somewhat automated.  The way it works is you create a file named slug_redirects.xml in your App_Data folder.  The contents of the XML looks like:

  <slug old="old-slug-1" new="replacement-slug-1" />
  <slug old="different-slug" new="twests" />

... where the "old" attribute contains the old slug and the "new" attribute contains the new/replacement slug.  You would need to manually add entries into this file.

Then in the UrlRewrite.cs file (in the BE core), you would want to replace the existing "RewritePost" method and add the new ReplacementSlug() method listed below.  Recompiling the core is required.

// REPLACE Existing "RewritePost" method in UrlRewrite.cs

private static void RewritePost(HttpContext context, string url)
    DateTime date = ExtractDate(context);
    string slug = ExtractTitle(context, url);

    string replacementSlug = ReplacementSlug(slug);
    if (!string.IsNullOrEmpty(replacementSlug))
        slug = replacementSlug;

    Post post = Post.Posts.Find(delegate(Post p)
        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;

        return slug.Equals(Utils.RemoveIllegalCharacters(p.Slug), StringComparison.OrdinalIgnoreCase);

    if (post != null && !string.IsNullOrEmpty(replacementSlug))
        context.Response.StatusCode = 301;
        context.Response.AppendHeader("location", post.AbsoluteLink.ToString());

    if (post != null)
        if (url.Contains("/FEED/"))
            context.RewritePath(Utils.RelativeWebRoot + "syndication.axd?post=" + post.Id.ToString() + GetQueryString(context), false);
            context.RewritePath(Utils.RelativeWebRoot + "post.aspx?id=" + post.Id.ToString() + GetQueryString(context), false);

// NEW Function in UrlRewrite.cs

private static string ReplacementSlug(string slug)
    string ReplacementSlugsCacheKey = "ReplacementSlugs";
    System.Xml.XmlDocument doc = HttpContext.Current.Cache[ReplacementSlugsCacheKey] as System.Xml.XmlDocument;

    if (doc == null)
        doc = new System.Xml.XmlDocument();

        string file = System.IO.Path.Combine(BlogSettings.Instance.StorageLocation, "slug_redirects.xml");
        file = System.Web.Hosting.HostingEnvironment.MapPath(file);

        if (System.IO.File.Exists(file))
            HttpContext.Current.Cache.Insert(ReplacementSlugsCacheKey, doc, new System.Web.Caching.CacheDependency(file));

    if (doc.HasChildNodes)
        System.Xml.XmlNode node = doc.SelectSingleNode("SlugRedirects/slug[@old='" + slug + "']");
        if (node != null)
            return node.Attributes["new"].Value;
    return null;
Mar 3, 2009 at 3:40 PM

How do I change the text on the contact form for the labels

Example. The contact form has fields for- name , email, subject and message.

I want to change text label – Subject - to say Telephone


Mar 3, 2009 at 4:25 PM
willweb: There's two ways to change the "Subject" label.

(1) You can edit the labels.resx file in the App_GlobalResources folder.  In that file, you will see:

<data name="subject" xml:space="preserve">

You would change <value>Subject</value> to <value>Telephone</value>.

(2) In the contact.aspx page in the root of your blog, you will find:

<%=Resources.labels.subject %>

You can replace that entire snippet with just the word "Telephone".

Incidentally, I'm not sure if you're aware that the last couple of times you had questions here in the CodePlex discussions, you've been asking your questions in the middle of someone else's discussion thread.  That's okay if your question is related to the original poster's question.  Otherwise, it would be better if you created a new Discussion thread ... thx.