2

我在 www.mydomain.com 有我的网站 asp 网页,而博客位于 www.mydomain.com/blog 内。我不得不将博客移动到一个子域,所以我需要编写一个模块来将博客内任何页面的任何调用重定向到新的子域。

它在我的机器上工作,但当我上传到共享主机时却不行。有什么想法有什么问题吗?

我写了以下代码

    public void ProcessRequest(HttpContext context)
{
    string sourceUrl =  @"www.mydomain.com/blog";// @"localhost:51393/blog"
    string destinationUrl = @"blog.mydomain.com/blog"; 
    string currentLocation = context.Request.Url.AbsoluteUri;
   if(currentLocation.ToLower().Contains(sourceUrl))
   {
       System.Web.HttpContext.Current.Response.Clear();
       System.Web.HttpContext.Current.Response.StatusCode = 301;
       System.Web.HttpContext.Current.Response.AddHeader("Location", currentLocation.Replace(sourceUrl, destinationUrl));
       System.Web.HttpContext.Current.Response.End();           
   }
}

并添加了这些处理程序

 <httpHandlers>
  <add verb="*" path="*.aspx" type="System.Web.UI.PageHandlerFactory" />
  <add verb="*" path="*" type="MyHandler,App_Code.dll"/>

任何帮助都深表感谢。

我知道它与这个httpHandler - 子文件夹问题非常相似,但它不起作用。

4

1 回答 1

2

只需使用 IIS 重写

这是一个将 1 个 URL 重定向到另一个 URL 的类似代码。稍微调整一下就可以了。我用我的 smarterasp.net 托管帐户得到了这个。

<configuration>
  <system.webServer>
    <rewrite>
      <rules>
        <rule name="Redirect to WWW" stopProcessing="true">
          <match url=".*" />
          <conditions>
            <add input="{HTTP_HOST}" pattern="^domain.com$" />
          </conditions>
          <action type="Redirect" url="http://www.domain.com/{R:0}"
               redirectType="Permanent" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>
</configuration>
于 2012-09-19T18:40:14.487 回答