【问题标题】:Redirect subfolder using HttpHandler使用 HttpHandler 重定向子文件夹
【发布时间】:2012-09-11 09:47:04
【问题描述】:

我在 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 - subfolder issue 非常相似,但它不起作用。

【问题讨论】:

    标签: c# asp.net httphandler


    【解决方案1】:

    只需使用 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>
    

    【讨论】:

    • 这个看起来很有希望。让我试一试。
    • 有点工作,但必须安装 URLRewriter 模块。它适用于 aspx 页面,但我需要它适用于所有页面。有什么想法吗?
    • 我将此标记为答案,因为它引导我走上了正确的道路。但是,我现在遇到了另一个问题,即我正在处理 stackoverflow.com/questions/12505834/… 的不存在页面
    猜你喜欢
    • 2013-12-24
    • 2011-02-12
    • 2015-05-27
    • 1970-01-01
    • 2013-10-16
    • 2017-09-08
    • 2021-09-08
    • 2018-09-30
    • 2014-04-03
    相关资源
    最近更新 更多