Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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

share|improve this question

1 Answer 1

up vote 4 down vote accepted

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|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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