Jimmyt1988
Jimmyt1988

Reputation: 21186

Replace a word in web.config url rewrite

I would like to replace any mention of a word in a url to become something else:

for example, the following:

"something/abc/342", "abc/hithere/something/2"

would rewrite to:

"REPLACED/abc/342", "abc/hithere/REPLACED/2"

I'm not entirely sure how it works but it might look something like this:

<rewrite>
    <rules>
        <rule name="Rewrite something to REPLACED">
            <match url="^/(something)/gi" />
            <action type="Rewrite" url="{putbackinanything}REPLACED{putbackinanything}" />
        </rule>
    </rules>
</rewrite>

Upvotes: 2

Views: 2363

Answers (1)

Mark
Mark

Reputation: 1371

You can use {R:X} where X is the number of the match-part that you want to put there.

    <match url="(.*)(something)(.*)" />
    <action type="Rewrite" url="{R:1}REPLACED{R:3}" />

You have three groups: The part in front of "something" ({R:1}), "something" itself ({R:2}) and the part behind "something" ({R:3}).

TIP: If you use the IIS Manager UI you can easily test and fine tune your pattern and see which R:X gives what:

enter image description here

Upvotes: 6

Related Questions