Question

I am trying to create URL rewrite inbound rule for IIS to return the URL before a specific parameter.

Edited: I should have stated the authProvider is always the last parameter.

Example:

http://localhost/WebAccess/Default.ashx?accessionNumber=009&authProvider=Bypass

I want to trim off &authProvider=Bypass from the end of the URL

I've tried:

.*(?=&authProvider)

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="Test" enabled="true">
                    <match url="(.*)&amp;authProvider.+" />
                    <action type="Rewrite" url="{R:0}" logRewrittenUrl="true" />
                </rule>
            </rules>
        </rewrite>

    </system.webServer>
</configuration>

enter image description here

Was it helpful?

Solution

The example that you have shown will match what you want, but not change the URL. The result of your match should have the matched string that you want as the result.

I think that the problem may be that you are trying to replace what you match, but since the forward lookup (?= is not part of the match result) when you do the replace you are ending up with the same string as when you started. As an alternative, assuming that you are aware that this will not be very robust if parameter orders change, you could use:

(.*)&authProvider.+

Then replace with

$1

This will result in:

http://localhost/WebAccess/Default.ashx?accessionNumber=009

Essentially it matches the whole string and replaces it with everything before $auth, which is in group 1 ($1).

Update

With your update, I see that the rewrite rule syntax uses {R:1} so $1 should be {R:1} in my example and in your Rewrite Rule should be {R:1}. See here for an example.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top