Is it possible to use URL Rewrite to provide more complex query string functionality than the "Append query string" checkbox that it has? Specifically, is it possible to specify the keys for certain query string parameters and have it only append those name value pairs.

For example, for the input:


and the list of query string parameter keys: beta gamma

it should output: http://www.example.org/redirect?beta=2&gamma=3

(Note that the query string parameters in the input appear in arbitrary order.)


My solution would be use conditions for that. By matching the conditions against the {QUERY_STRING} you can use back references to use them in the redirect URL.

<?xml version="1.0" encoding="UTF-8"?>
                <rule name="Redirect" stopProcessing="true">
                    <match url="(.*)" />
                    <conditions trackAllCaptures="true">
                        <add input="{QUERY_STRING}" pattern="&amp;?(beta=[^&amp;]+)&amp;?" />
                        <add input="{QUERY_STRING}" pattern="&amp;?(gamma=[^&amp;]+)&amp;?" />
                        <add input="{REQUEST_URI}" pattern="^/redirect" negate="true" />
                    <action type="Redirect" url="/redirect?{C:1}&amp;{C:2}" appendQueryString="false" redirectType="Found" />

The only possible problem with this solution might be (depending on what you want) is that the redirect will only happen if both the beta and gamma query string variables are present in the query string. If they are not, the redirect will not happen.

The redirect rule matches against any URL ((.*)). If needed, you can change that. I've also added an extra condition to not have the rule match against the redirect URL itself which would otherwise cause the redirect URL to be redirected itself.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.