Using Url Rewrite, I'm trying to redirect /foo_bar/* to /foo/*. I've tried this:

<rule name="Redirect foo_bar to foo" stopProcessing="true">
    <match url="(.*)" />
    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
        <add input="{HTTP_URL}" pattern="^/foo_bar/(.*)" />
    <action type="Redirect" url="/foo/{R:1}" />

But this just redirects to /foo/foo_bar/*. Where am I going wrong?


First of all, you specify a rule to match all requests (.*), then you add a condition for a specific url.

You should have your limiting criteria in the match:

<match url="^/foo_bar/(.*)" />

then you don't need the condition anymore. You use conditions only for additional criteria not based on the URL.

Secondly {R:1} refers back to the whole url from the match, so if you have /foo_bar/ in the url thats what is in {R:1}, that explain why it redirects to /foo/foo_bar/.

With your new match rule, {R:1} holds what's in (.*), not the whole Url, so it should work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.