【发布时间】:2011-02-12 12:11:43
【问题描述】:
我正在尝试将我的旧打字板博客重定向到使用 wordpress 运行的新博客(永久 301 重定向)。新博客也将在新服务器上。
旧博客的结构如下: http://subdomain.domain.com/weblog/year/month/what-ever-article.html
新博客如下所示: http://www.domain.com/Blog/index.php/year/month/what-ever-article.html
我正在使用我在网上找到并尝试使用它的 http 处理程序:
public class MyHttpModule :IHttpModule
{
public MyHttpModule()
{
//
// TODO: Add constructor logic here
//
}
#region IHttpModule Members
public void Dispose()
{
}
public void Init(HttpApplication context)
{
context.BeginRequest += new EventHandler(context_BeginRequest);
}
void context_BeginRequest(object sender, EventArgs e)
{
string oldURL = System.Web.HttpContext.Current.Request.Url.ToString();
string newURL = String.Empty;
//oldURL =
if (oldURL.ToString().ToLower().IndexOf("articles") >= 0 || System.Web.HttpContext.Current.Request.Url.ToString().ToLower().IndexOf("weblog") >= 0)
{
newURL = oldURL.Replace("subdomain.domain.com/weblog", "www.domain.com/Blog/index.php");
if (newURL.ToLower().Contains("subdomain"))
{
newURL = "http://www.domain.com/Blog";
}
}
else
{
newURL = "http://www.domain.com/Blog";
}
System.Web.HttpContext.Current.Response.Clear();
System.Web.HttpContext.Current.Response.StatusCode = 301;
System.Web.HttpContext.Current.Response.AddHeader("Location", newURL);
System.Web.HttpContext.Current.Response.End();
}
#endregion
}
为了使用此代码,我将处理程序放入 web.config 中
<httpModules>
<add name="MyHttpModule" type="MyHttpModule, App_Code"/>
</httpModules>
我遇到的问题是,当我想从 http://subdomain.domain.com/weblog/year/month/what-ever-article.html 重定向时,我收到一个错误,指出该文件夹不存在。
有什么方法可以更改我的脚本或将全部捕获添加到将 URL 转发到我的脚本的 web.config 中?
当我在 oldURL 字符串中使用“http://subdomain.domain.com/weblog/year/month/what-ever-article.html”时,重定向工作得很好......所以我一定有一些 IIS 或 web.config 设置错误。
提前致谢, 帕特里克
【问题讨论】:
-
为此使用 IIS 重写器模块(假设您使用的是 IIS7)不是更好吗?
标签: c# asp.net httphandler http-status-code-301