将错误的网址和域重定向到特定网页

时间:2014-02-05 11:40:44

标签: asp.net asp.net-mvc

我想重定向以下网址:

http://ABC.mywebsite.com/

到此网址:

http://mywebsite.com/error.html

所以我尝试了下面列出的选项,但自定义错误标记无效:

<customErrors mode="On" defaultRedirect="Error.html">

</customErrors>

这也不起作用:

    <customErrors mode="On" defaultRedirect="Error.html">

        <error statusCode="204" redirect="Default.aspx" />
        <error statusCode="400" redirect="Default.aspx" />
        <error statusCode="403" redirect="Default.aspx" />
        <error statusCode="404" redirect="Default.aspx" />

    </customErrors>

以下代码也无效:

    protected void Application_Error(object sender, EventArgs e)
    {


       Exception exc = Server.GetLastError();

        if (exc is HttpUnhandledException)
        {
            // Pass the error on to the error page.
            Server.Transfer("Error.html");
        }   

    }

如何解决我的问题,我认为问题不在应用程序上下文中,也许是在IIS上下文中,非常感谢。

1 个答案:

答案 0 :(得分:1)

使用IIS URL重写模块可能是最佳选择。

您需要在IIS服务器上安装它:http://www.iis.net/downloads/microsoft/url-rewrite

这里有很好的教程:

http://www.iis.net/learn/extensions/url-rewrite-module

然后,您可以尝试使用类似于以下内容的配置(将其添加到您的web.config中):

<system.webServer>
    <rewrite>
        <rules>
            <rule name="Redirect to error" stopProcessing="true">
                <match url=".*" />
                <conditions>
                    <add input="{HTTP_HOST}" pattern="^ABC\.mywebsite\.com$" />
                </conditions>
                <action type="Redirect" url="http://mywebsite.com/error.html" />
            </rule>
        </rules>
    </rewrite>
</system.webServer>

希望对你有所帮助。