阻止无效的URL并重定向到IIS中的主页

时间:2014-11-27 08:00:39

标签: asp.net iis url-rewriting dns

目前我正在使用其他公司提供的网络托管服务。它使用IIS 7 / IIS 8,允许客户根据需要修改web.config

假设我设置了两个有效的网址:

  1. http://www.example.com
  2. http://dev.example.com
  3. 现在,当我尝试访问http://abc.example.com时,会返回HTTP Error 403.14 - Forbidden;如果我然后尝试访问http://www.example.com/abc,则会返回HTTP Error 404.0 - Not Found

    如何配置web.config,以便用户在尝试访问无效网址时重定向到http://www.example.com

    我曾尝试使用该公司提供的代码段,但没有成功:

    <?xml version="1.0" encoding="UTF-8"?>
    <configuration>
        <system.webServer>
            <rewrite>
                <rules>
                    <rule name="Canonical Host Name" stopProcessing="true">
                      <match url="(.*)" />
                      <conditions>
                        <add input="{HTTP_HOST}" pattern="^example\.com$" />
                      </conditions>
                      <action type="Redirect" url="http://www.example.com/{R:1}" redirectType="Permanent" />
                    </rule>
                 </rules>
            </rewrite>
        </system.webServer>
    </configuration>
    

1 个答案:

答案 0 :(得分:1)

在webconfig标签的system.web中添加此标记

<customErrors mode="On" defaultRedirect="~/Home/Index"> 
 </customErrors>

每当发生错误时,都会将您重定向到主页 通过添加customErrors标记

,您可以更具体地进行重定向
<error statusCode="401" redirect="/error/otherpage"/>

这将指示在401错误发生时您将被重定向到的页面