2014-02-05 77 views
0

我想下面的URL重定向:重定向錯誤的網址或域名指定

http://ABC.mywebsite.com/ 

此URL:

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背景下,得益於adance。

回答

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> 

希望可以幫到你。