regexiisurl-rewritingiis-7url-routing

Regular Expression "^$" For URL Rewrite in IIS


I have the following regular expression used for pattern matching in a URL rewrite in IIS that was set up by my predecessor:

 ^$

I know very little regex, and the reading I've done this morning hasn't helped me in figuring out how this one works.

It is rewriting:

 http://dittest/

to:

 http://dittest:8080/dit

Why does the ^$ regex work in testing the URL http://dittest/ and rewriting to http://dittest:8080/dit if ^$ is supposed to match an empty string?

This question is not a duplicate, because it's not asking what the regex means – rather why it works in using it for an inbound rule URL rewrite in IIS when it doesn't work to validate http://dittest/.

I appreciate the help. Thank you!


Solution

  • Yeah, is an old post, but... doesn't appear to really have an answer. Although @Adam-Mazzarella (https://stackoverflow.com/users/5169684/adam-mazzarella) did mention the answer in passing.

    The key is that IIS URL Rewrite uses only the PATH portion of the input URL to match against. That is the part of the URL that comes after the slash of the host:port portion, and does not include the host:port portion.

    MS documentation: https://learn.microsoft.com/en-us/iis/extensions/url-rewrite-module/url-rewrite-module-configuration-reference#accessing-url-parts-from-a-rewrite-rule

    From that:

    For an HTTP URL in this form: http(s)://<host>:<port>/<path>?<querystring>      
        - The <path> is matched against the pattern of the rule...
    

    So, from your example, the input is: "http://dittest:8080/" (or "http://dittest" should result in the same match):

    ... this would match, and rewrite the URL.

    If your input URL was "http://dittest:8080/some/thing" or "http://dittest/some/thing":

    ... this would not match because the Path is not null, and thus would not rewrite the URL.