: Web.config -> Redirect old blog URL's to new pattern 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.
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:
domain.tld/blog/post/2011/03/02/my-blog-post.aspx
Now I want to redirect this url to:
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.
More posts by @Shelley277
1 Comments
Sorted by latest first Latest Oldest Best
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
Terms of Use Create Support ticket Your support tickets Stock Market News! © vmapp.org2024 All Rights reserved.