Question

This is a fairly newbie URL Rewrite matching question. I want to be able to match all URIs which follow the pattern and redirect all such matches to a single place.

The following URLs should match:

www.mydomain.com/foo/*

and

www.mydomain.com/foo

All the above should redirect to www.mydomain.com/xyz/abc/something.html

So, the following should match:

www.mydomain.com/foo
www.mydomain.com/foo/bar
www.mydomain.com/foo/bar/baz.html

And the following should not match:

www.mydomain.com/blah/foo
www.mydomain.com/bloo/foo/blee
www.mydomain.com/bling/foo/blurp/blomp.html

I (perhaps naively) set up this rule:

<rule name="Rule1" stopProcessing="true">
  <match url="foo/(.*)" />
  <action type="Redirect" url="/xyz/abc/something.html" appendQueryString="true" redirectType="Permanent"/>
</rule>

This rule is working correctly for

www.mydomain.com/foo/
www.mydomain.com/foo/bar

But it is not firing for

www.mydomain.com/foo (note missing trailing slash)

And it is falsely firing for

www.mydomain.com/anything/foo/

How can I ensure that the rule fires only for URIs which begin with /foo and also fires for URIs which begin with /foo and do not have a trailing slash?

Thanks in advance.

Was it helpful?

Solution

This rule will do the job:

<match url="(^foo/(.*))|(^foo$)" />

This ^ will ensure it matches foo always.

This will match

www.mydomain.com/foo  (with missing trail)
www.mydomain.com/foo/ 
www.mydomain.com/foo/bar    
www.mydomain.com/foo/bar/baz.html

Won't match anything

www.mydomain.com/anything/foo/
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top