简体   繁体   中英

How to redirect all requests excepts few ones, using IIS Rewrite Module

I am trying to allow only few URLs on concrete domain, all other requests should be redirected to 404 Not Found. I know how to redirect a concrete URL to 404, but I need like an inversion of this. How do I write such rule, what conditions should I use? Is there something like not match in rule syntax?

So, what I am currently using:

<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>

But as there are now too many of such rules, so that I can even miss some of them, And that's why I want to simplify a bit, so that I allow some URLs, and redirect all others to NotFound.

Ok, I've found out an answer. The key is negate="true" in condition :

<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>

If urls to allow more than one - add more conditions and put logicalGrouping="MatchAll" to conditions node.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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