如何使用IIS重写模块重定向除少数请求外的所有请求

本文关键字:请求 IIS 何使用 重写 模块 重定向 | 更新日期: 2023-09-27 18:13:37

我试图在具体的域上只允许几个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,并将所有其他的重定向到NotFound.

如何使用IIS重写模块重定向除少数请求外的所有请求

好了,我找到答案了。在条件

下,键为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节点