如何重定向除了少数几个请求,使用IIS重写模块
问题描述:
我想只允许几个具体域上的URL,所有其他请求应重定向到404未找到。 我知道如何将一个具体的URL重定向到404,但我需要像这样的反转。我如何编写这样的规则,我应该使用什么条件?规则语法中是否有类似not match
?如何重定向除了少数几个请求,使用IIS重写模块
所以,我现在使用的是:
<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>
但由于现在有太多的这样的规则,让我甚至可以错过其中的一部分,这就是为什么我想简化了一下,以便我允许一些网址,并将所有其他网址重定向到NotFound。
答
好的,我找到了答案。关键是在条件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
节点。