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.

I need to create a rewrite rule as follows:

OldPage.aspx?UserId=12345 should redirect to NewPage.aspx?Id=12345

the '12345' can be any numeric value.)

I have the page redirect working, but not sure how to change the 'UserId' to 'Id' (I can do it in my code, but would rather do it in the web.config).

This is what I have:

    <rewrite>
        <rules>
            <rule name="Old Site to New" patternSyntax="Wildcard" stopProcessing="true">
                <match url="*OldPage.aspx*" />
                <action type="Rewrite" url="NewPage.aspx" appendQueryString="true" logRewrittenUrl="true" />
            </rule>
        </rules>
    </rewrite>

I am using wildcards, but a suggestion with either regex or wildcards would be OK by me.

share|improve this question

1 Answer 1

up vote 2 down vote accepted

This should do the job based on your URL examples:

<rule name="Old2New">
    <match url="^OldPage\.aspx$" />
    <conditions>
        <add input="{QUERY_STRING}" pattern="UserId=(\d+)" />
    </conditions>
    <action type="Rewrite" url="NewPage.aspx?Id={C:1}" appendQueryString="false" />
</rule>

The common mistake that a lot of people do is trying to match whole URL including query string. The reality is: when matching URL, the pattern get applied to path part of it and query string has to be matched separately.

share|improve this answer
    
Works perfectly...sometimes you just need a working example to see the light...and you are right, I was trying to match the whole URL –  EJB Aug 9 '11 at 23:51

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.