Is this URL Rewrite regular expresssion match get the same result as a string split on '/'?
-
13-11-2019 - |
Question
I'm looking into replacing a PHP redirect script that splits the query string on '/' with an IIS 7 URL rewrite 2.0 rule.
Here's the code that extracts information from the request in the original redirect script:
list($campaign,$banner,$add_code,$deploy,$opt) = split("/",$_SERVER['QUERY_STRING']);
And here's what I think can replace that:
<rule name="MyRule">
<match url="(.*?)/(.*?)/(.*?)/(.*?)/(.*)" />
<action type="Redirect" url="TestRedirectTarget.aspx/?campaign={R:1}&banner={R:2}&add_code={R:3}&deploy={R:4}&opt={R:5}" />
</rule>
I'm asking if there's anything I am not thinking of in terms of the matching regex being the same functionality as the PHP string-split-assiged-to-list logic. I found an issue such that I had to use lazy matching to handle the case that there are more than 5 pieces of slash-separated-url. I'm wondering if there's other cases where the result would not be the same.
Solution
your match expr differs wrt urls that contain the server name/address (mapping mismatch) and whose qs contains less than 5 parts (potentially no match at all).
to catch these conditions use url attributes as follows:
\?([^/]*)($|/([^/]*)($|/([^/]*)($|/([^/]*)($|/(.*)))))
-> TestRedirectTarget.aspx/?campaign={R:1}&banner={R:3}&add_code={R:5}&??;deploy={R:7}&opt={R:9}
.