如何将自定义URl重定向到ASP.Net IIS 7.5中的特定端口
我已经创建了两个站点,一个在端口80上,这是默认站点,另一个在端口786上,现在我想,如果用户打开url,它应该打开。我已经安装了IIS 7.5重写模块。例如,我用不同的方法编写代码如何将自定义URl重定向到ASP.Net IIS 7.5中的特定端口,asp.net,.net,url-rewriting,iis-7.5,Asp.net,.net,Url Rewriting,Iis 7.5,我已经创建了两个站点,一个在端口80上,这是默认站点,另一个在端口786上,现在我想,如果用户打开url,它应该打开。我已经安装了IIS 7.5重写模块。例如,我用不同的方法编写代码 <rule name="Red" stopProcessing="true"> <match url=".*" /> <conditions logicalGrouping="MatchAll"> <
<rule name="Red" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTP_HOST}" pattern="www.myurl.com/Application" />
</conditions>
<action type="Rewrite" url="http://{HTTP_HOST}:786/{R:0}" />
</rule>
我认为它像一个301永久重定向。 您可以使用HttpModule来执行此操作
public class CustomHttpModule : IHttpModule
{
private HttpApplication app;
public void Init(HttpApplication context)
{
app = context;
app.AuthorizeRequest += App_AuthorizeRequest;
}
public void App_AuthorizeRequest(object sender, EventArgs e)
{
HttpRequest req = app.Request;
string path = req.Path;
// if starts with http://{HTTP_HOST}
if (path.StartsWith("http://{HTTP_HOST}", true, CultureInfo.CurrentCulture))
{
string redirectUrl = path.Replace("http://{HTTP_HOST}", "http://{HTTP_HOST}:786");
app.Response.StatusCode = 301;
app.Response.AddHeader("Location", redirectUrl);
app.Response.End();
}
}
public void Dispose()
{
}
}
此外,您还需要在system.webserver节点和system.web节点中配置此HttpModule。
<add input="{REQUEST_URI}" matchType="Pattern" pattern="^/Application$" ignoreCase="true" negate="false" />
这只能解决问题