Dan LaRiviere
Dan LaRiviere

Reputation: 11

IIS URL Rewrite Rule with Params

I am having a hard time getting a URL rewrite rule to work.

I want this url:

http://www.mysite.com/oldpage.aspx?oldid=123

To rewrite to:

http://www.mysite.com/sub/newpage.aspx?newid=123

Here is what I have, but it's not working:

<rule name="Old2New" stopProcessing="true">
    <match url="^oldpage.aspx?oldid=([0-9]+)" />
    <action type="Rewrite" 
            url="/sub/newpage.aspx?newid={R:1}" 
            appendQueryString="true"  />
</rule>

What am I missing?

Upvotes: 1

Views: 6185

Answers (2)

Ralpharama
Ralpharama

Reputation: 461

Use the conditions to catch the query string part {C:1}, like this:

<rule name="My rule" stopProcessing="true">
    <match url="^oldpage\.aspx" ignoreCase="true" />
    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
        <add input="{QUERY_STRING}" pattern="oldid=([0-9]+)" />
    </conditions>
    <action type="Redirect" url="/sub/newpage.aspx?newid={C:1}" appendQueryString="false" />
</rule>

Tested and working.

Upvotes: 2

Victor
Victor

Reputation: 4721

The regex index starts at 0 not 1. so your rule should be:

<rule name="Old2New" stopProcessing="true">
    <match url="^oldpage.aspx?oldid=([0-9]+)" />
    <action type="Rewrite" 
            url="/sub/newpage.aspx?newid={R:0}" 
            appendQueryString="true"  />
</rule>

You can easily test your rule in IIS7 interface.

Upvotes: 0

Related Questions