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

时间:2016-10-19 07:54:24

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

我试图在具体域上仅允许少量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。

1 个答案:

答案 0 :(得分:0)

好的,我找到了答案。条件为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>

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