IIS URL Rewrite Module Query String Parameters

36,706

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"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <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" />
                    </conditions>
                    <action type="Redirect" url="/redirect?{C:1}&amp;{C:2}" appendQueryString="false" redirectType="Found" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>

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.

Share:
36,706

Related videos on Youtube

Jason Kresowaty
Author by

Jason Kresowaty

Updated on September 18, 2022

Comments

  • Jason Kresowaty
    Jason Kresowaty almost 2 years

    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:

    http://www.example.org/test?alpha=1&beta=2&gamma=3

    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.)