Teeknow
Teeknow

Reputation: 1015

IIS Rewrite Safely Add Custom Query String

I want to move something in a url path out to the query string. This is almost like a subdomain rewrite. Here's what the rewrite should look like: ~/ch-ar/rest -> ~/rest?dom=ch-ar ~/su-bd/rest?param=val -> ~/rest?param=val&dom=su-bd

I can definitely do the first part through matching/replacing isn't something I have been able to account for. I'm not sure if there's some way to safely add my key value pair to the {QUERY_STRING} variable but that would probably be ideal. The key for this name value pair will never change and the order of the query string doesn't matter to me.

Upvotes: 0

Views: 538

Answers (1)

lat3ncy
lat3ncy

Reputation: 166

I would suggest using the following 2 rules, one for requests that have a query string and one for requests that do not. I have used a language specific URI stem as an example and have set the action to redirect so it is easier to test.

            <rule name="Language URI to QS - Existing Query String" stopProcessing="true">
                <match url="^([a-z]{2}-[a-z]{2})(.*)" />
                <conditions>
                    <add input="{QUERY_STRING}" pattern="(.+)" />
                </conditions>
                <serverVariables>
                    <set name="QUERY_STRING" value="{C:1}&amp;dom={R:1}" />
                </serverVariables>
                <action type="Redirect" url="{R:2}" appendQueryString="true" redirectType="Found" />
            </rule>

            <rule name="Language URI to QS - No Query String" stopProcessing="true">
                <match url="^([a-z]{2}-[a-z]{2})(.*)" />
                <conditions>
                    <add input="{QUERY_STRING}" pattern=".+" negate="true" />
                </conditions>
                <serverVariables>
                    <set name="QUERY_STRING" value="dom={R:1}" />
                </serverVariables>
                <action type="Redirect" url="{R:2}" appendQueryString="true" redirectType="Found" />
            </rule>

Upvotes: 1

Related Questions