【问题标题】:How to redirect all requests excepts few ones, using IIS Rewrite Module如何使用 IIS 重写模块重定向除少数请求之外的所有请求
【发布时间】:2017-02-28 17:40:16
【问题描述】:

我试图在具体域上只允许几个 URL,所有其他请求都应该重定向到 404 Not Found。 我知道如何将具体的 URL 重定向到 404,但我需要对此进行反转。我该如何写这样的规则,我应该使用什么条件?规则语法中有not match之类的东西吗?

那么,我目前使用的是什么:

<rule name="Deny some request" enabled="true" stopProcessing="true">
      <match url="^(.*)$" />
      <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
        <add input="{HTTP_HOST}" pattern="(.*)(\.domain_regexp_here.*)$" negate="true" />
        <add input="{REQUEST_URI}" pattern="(.*)(somename)$" />
      </conditions>
      <action type="Redirect" url="/Home/PageNotFound" appendQueryString="false" />
    </rule>

但是由于现在这样的规则太多了,以至于我什至会错过其中的一些,这就是为什么我想稍微简化一下,以便我允许一些 URL,并将所有其他 URL 重定向到 NotFound。

【问题讨论】:

    标签: c# .net iis web-config url-rewrite-module


    【解决方案1】:

    好的,我找到了答案。关键是negate="true" 条件:

    <rule name="RequestBlockingRule1" stopProcessing="true">
                    <match url=".*" />
                    <conditions>
                        <add input="{URL}" pattern="url path" negate="true" />
                    </conditions>
                    <action type="CustomResponse" statusCode="404" statusReason="File or directory not found." statusDescription="The resource you are looking for might have been removed, had its name changed, or is temporarily unavailable." />
                </rule>
    

    如果 url 允许多个 - 添加更多条件并将 logicalGrouping="MatchAll" 放入 conditions 节点。

    【讨论】:

      猜你喜欢
      • 2019-10-03
      • 2018-11-29
      • 2016-10-17
      • 1970-01-01
      • 2021-11-25
      • 2012-11-04
      • 1970-01-01
      • 2020-12-14
      • 1970-01-01
      相关资源
      最近更新 更多