Bogdan Daniel
Bogdan Daniel

Reputation: 2749

Use Regex to lookahead and redirect in case of match IIS

I have the following IIS rule which is supposed to redirect if the URI does not contain the word Api:

         <rule name="React Routes" stopProcessing="true">
            <match url=".*" />
            <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                <add input="{REQUEST_URI}" pattern="^((?!Api).)*$" negate="false" />
            </conditions>
            <action type="Rewrite" url="/" />
        </rule>

This was working fine until I added a token as a query parameter for a route. Now when it tries to match that URI it will go out of memory.

How would I have to write the pattern so it looks only in the first 30 characters? The /Api/ route will never appear later. This way I will make sure that the regular expression matching does not run out of memory when a token is present.

Upvotes: 1

Views: 314

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626691

To make sure Api does not occur within the first 30 chars you may use

pattern="^(?!.{0,27}Api).*"

Details

  • ^ - start of string
  • (?!.{0,27}Api) - a negative looakahead that matches a location that is not immediately followed with any 0 to 27 chars (other than linebreak chars) and Api after them
  • .* - any 0+ chars (other than linebreak chars).

Upvotes: 1

Related Questions