新建一个类:RedirectModule
{
void IHttpModule.Init(HttpApplication context)
{
context.AuthorizeRequest += context_AuthorizeRequest;
}
{
var app = sender as HttpApplication;
string indexIF = app.Request.Url.PathAndQuery.TrimStart('/');
if (indexIF == "map_index.action")
{
app.Response.StatusCode = 301;
app.Response.AddHeader("Location", "http://www.baidu.com/");
}
else if (indexIF.Length > 0)
{
if (indexIF.Substring(indexIF.Length - 2, 2) == "js")
{
app.Response.StatusCode = 404;
app.Response.AddHeader("Location", "http://www.baidu.com/");
app.Response.AddHeader("Location", "http://www.baidu.com/");
}
if (indexIF.Substring(indexIF.Length - 3, 3) == "css")
{
app.Response.StatusCode = 404;
app.Response.AddHeader("Location", "http://www.baidu.com/");
app.Response.AddHeader("Location", "http://www.baidu.com/");
}
else
{
app.Response.StatusCode = 301;
app.Response.AddHeader("Location", "http://www.baidu.com/" + indexIF);
}
app.Response.End();
}
void IHttpModule.Dispose() { }
}
配置相应的web.config
<customErrors mode="RemoteOnly" redirectMode="ResponseRewrite" defaultRedirect="~/errorPage/ErrorPage.htm">
<error statusCode="403" redirect="~/errorPage/HTMLPage2.htm"/>
<error statusCode="404" redirect="~/errorPage/HTMLPage.htm"/>
<error statusCode="302" redirect="~/errorPage/HTMLPage3.htm"/>
<error statusCode="400" redirect="~/errorPage/HTMLPage4.htm"/>
<error statusCode="401" redirect="~/errorPage/HTMLPage5.htm"/>
<error statusCode="408" redirect="~/errorPage/HTMLPage6.htm"/>
<error statusCode="414" redirect="~/errorPage/HTMLPage7.htm"/>
<error statusCode="500" redirect="~/errorPage/HTMLPage8.htm"/>
<error statusCode="503" redirect="~/errorPage/HTMLPage9.htm"/>
<error statusCode="505" redirect="~/errorPage/HTMLPage10.htm"/>
</customErrors>
</system.web>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true">
<add name="redirectModule" type="WebApplication1.RedirectModule"/>
</modules>
</system.webServer>