Take the 2-minute tour ×
Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

I just changed my blogging software and I have now the problem that I want to redirect my old imported blog post to the new url pattern.

My old pattern looks like:

www.domain.tld/blog/post/2011/03/02/my-blog-post.aspx

Now I want to redirect this url to:

www.domain.tld/blog/my-blog-post

How can I do this using the Web.config for a HTTP 301 Moved Permanently message? I just found solutions to redirect a single url, but not with dynamic placeholder. I need to remove the year, month and day part from the url and then cut off the file extension from the post name.

I came from Blogengine.Net and switched to Funnelweb. I have shared web space on a Windows Server 2008, so I've only FTP access.

Thank for any advice

Solution:

Add this to the Application_BeginRequest block:

UriBuilder uri = new System.UriBuilder(Context.Request.Url);
Regex r = new Regex(@"^/blog/post/\d+/\d+/\d+/([A-Za-z0-9\-]+)");
Match m = r.Match(uri.Path);
if (m.Success)
{
   String postName = m.Groups[1].ToString();
   uri.Path = "blog/" + postName;
   Response.Status = "301 Moved Permanently";
   Response.AddHeader("Location", uri.ToString());
   Context.ApplicationInstance.CompleteRequest();
}

Pleas let me know if you have improvements for this.

share|improve this question

migrated from serverfault.com Jan 14 '12 at 14:39

This question came from our site for professional system and network administrators.

    
Can you add the blog and web server software you are using? Do you have a shell access? –  jflaflamme Jan 14 '12 at 14:58
    
Sure, I added this information. –  mc-kay Jan 14 '12 at 15:52
add comment

1 Answer 1

up vote 0 down vote accepted

I do not know how to do it in web.config (I do not know if possible or not)

But I do know a way to do this in Global.asax

    protected void Application_BeginRequest(object sender, EventArgs e)
    {
        UriBuilder uri = new System.UriBuilder(Context.Request.Url);
        if (uri.Path == "/blog/post/2011/03/02/my-blog-post.aspx") uri.Path = "/blog/my-blog-post";
        Response.Status = "301 Moved Permanently";
        Response.AddHeader("Location", uri.ToString());
        Context.ApplicationInstance.CompleteRequest();
    }

For the dynamic part, you have to use Regex

if (!Regex.IsMatch(url.Path, @"^/blog/post/\d+/\d+.. sorry, have to go, will come back finish later

share|improve this answer
    
Thanks, that was a very good idea. –  mc-kay Jan 14 '12 at 18:59
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.