繁体   English   中英

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

[英]Blocking invalid URL and redirect to homepage in IIS

目前,我正在使用另一家公司提供的网络托管服务。 它使用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 ,以便在尝试访问无效URL时将用户重定向到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>

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

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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM