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}&amp;banner={R:2}&amp;add_code={R:3}&amp;deploy={R:4}&amp;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.

Was it helpful?

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}&amp;banner={R:3}&amp;add_code={R:5}&amp??;deploy={R:7}&amp;opt={R:9}.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top