我只是改变了我的博客软件,现在我有了一个问题,我想把我的旧的导入博客文章重定向到新的url模式。
我以前的模式是:
www.domain.tld/blog/post/2011/03/02/my-blog-post.aspx现在我想将这个网址重定向到:
www.domain.tld/blog/my-blog-post如何使用Web.config对HTTP 301 Moved Permanently消息执行此操作?我刚刚找到了重定向单个url的解决方案,但没有使用动态占位符。我需要从url中删除年、月和日部分,然后从post名称中删除文件扩展名。
我从Blogengine.Net来,转到了Funnelweb。我在Windows 2008上共享了web空间,所以我只能访问FTP。
谢谢你的建议
将其添加到Application_BeginRequest块中:
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();
}如果你在这方面有改进,请告诉我。
发布于 2012-01-14 15:57:10
我不知道如何在web.config中这样做(我不知道是否可能)
但我知道如何在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();
}对于动态部分,您必须使用Regex
如果(!Regex.IsMatch)(url.Path,@"^/blog/post/\d+/\d+.对不起,必须走了,过一会儿再回来
https://webmasters.stackexchange.com/questions/24556
复制相似问题