0
votes

I am trying for hours to get the following IIS rewrite to work:

Incoming URL:

https://example.com/services/control/status?Id=SO1234567

Expected output:

https://example.com/orders/detail/SO1234567

IIS Rewrite Rule:

<rule name="Custom rewrite rule" patternSyntax="ExactMatch" stopProcessing="true">
    <match url="^services/control/status\?Id=(SO|so){1}([0-9]*)" />
    <action type="Rewrite" url="/orders/detail/{R:0}" appendQueryString="false" logRewrittenUrl="true" />
</rule>

Additional information:

  • I am running a single web site in IIS.
  • 1 web applications for an API under this web site.
  • Angular files within the web site.

Angular rules:

<rule name="Angular Routes" stopProcessing="false">
    <match url=".*" />
    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
        <add input="{REQUEST_URI}" pattern="^/(api)" negate="true" />
    </conditions>
    <action type="Rewrite" url="/" />
</rule>

Complete Rewrite section:

<rewrite>
    <rules>
        <clear />
        <rule name="Angular Routes" stopProcessing="false">
            <match url=".*" />
            <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                <add input="{REQUEST_URI}" pattern="^/(api)" negate="true" />
                <add input="{REQUEST_URI}" pattern="^/(marketplace)" negate="true" />
            </conditions>
            <action type="Rewrite" url="/" />
        </rule>

        <rule name="Custom rewrite rule" patternSyntax="ExactMatch" stopProcessing="true">
    <match url="^services/control/status\?Id=(SO|so){1}([0-9]*)" />
    <action type="Rewrite" url="/orders/detail/{R:0}" appendQueryString="false" logRewrittenUrl="true" />
</rule>
</rule>
    </rules>        
</rewrite>
1

1 Answers

1
votes

As far as I know, the url pattern will not match the query string. If you want to get the query string value, you should use rewrite condition.

Details, you could refer to below rule:

            <rule name="QueryStringRue" stopProcessing="true">
                <match url=".*" />
                <conditions>
                    <add input="{QUERY_STRING}" pattern="^Id=([0-9a-zA-Z]*)" />
                </conditions>
                <action type="Redirect" url="https://example.com/orders/detail/{C:0}" />
            </rule>