我的网站 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 重定向到另一个 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/