gpt4 book ai didi

c# - 使用 HttpHandler 重定向子文件夹

转载 作者:太空宇宙 更新时间:2023-11-03 11:16:37 25 4
gpt4 key购买 nike

我的站点 asp 网页位于 www.mydomain.com,博客位于 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但它没有用。

最佳答案

只需使用 IIS 重写

这是将 1 个 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>

关于c# - 使用 HttpHandler 重定向子文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12488709/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com