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.