【问题标题】:Blocking invalid URL and redirect to homepage in IIS阻止无效 URL 并重定向到 IIS 中的主页
【发布时间】:2014-11-27 08:00:38
【问题描述】:

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

假设我设置了两个有效的 URL:

  1. http://www.example.com
  2. http://dev.example.com

现在,当我尝试访问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

我尝试使用公司提供的sn-p但没有成功:

<?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>

【问题讨论】:

标签: asp.net iis url-rewriting dns


【解决方案1】:

在 system.web 的 webconfig 标签中添加这个标签

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

每当发生错误时,这会将您重定向到主页 您可以通过添加 customErrors 标记来更具体地重定向

<error statusCode="401" redirect="/error/otherpage"/>

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

【讨论】:

  • 我刚刚尝试了&lt;customErrors defaultRedirect="http://www.example.com" mode="On"&gt;&lt;/customErrors&gt;,但没有成功。 403 在我访问无效 URL 时仍然返回。有什么线索吗?
  • 如果无效 url 以 example.com 开头,则此解决方案有效,因为它在 IIS 站点中托管的同一应用程序中进行重定向,因此在默认重定向中,您必须指定本地路径
猜你喜欢
  • 2017-01-10
  • 2019-04-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-30
  • 2020-07-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多