views:

36

answers:

1

We've got some incoming URLs that needs to be redirected, but we are having trouble with URLs that contains pluses (+).

For example any incoming URL must be redirected to the Homepage of the new site:

/eng/news/2005+01+01.htm

Should be redirected to to the home page of the new site

/en/

Using UrlRewriter.net we've set up a rule which works with 'normal' URLs but does not work for the above

<redirect url="~/eng/(.+)" to="/en/index.aspx" />

However it works fine if i change the incoming URL to

/eng/news/2005-01-01.htm

What's the problem and can anyone help?

A: 

I don't know about UrlRewriter.net, and I'm not sure which regex syntax it uses. I give some hint based on Perl regex.

  1. what is the ~ at the beginning? Perhaps you mean ^, i.e. beginning of the string.
  2. (.+) matches any character repeated one or more time; it does not match the + sign as you want

This is one way to write a (Perl) regex matching URLs starting with the string /eng/ and containg a + sign:

^\/eng\/.*\+.*

I hope this helps.

MarcoS
UrlRewriter.net uses the .NET regex syntax - where `.` *does* include the `+` character - just like every other regex flavour I've experienced.
Peter Boughton
Also, even if dot somehow doesn't include plus, using `.*\+.*` wont work when he has multiple `+` characters (as in example). Something like `[^\n]*` would be simpler
Peter Boughton
@Peter Boughton: why do you say it does not work? I think it does: If I do `perl -pi -e 'if (/^\/eng\/.*\+.*/) { print "yes\n"; } else { print "no\n"; }'`, and I give `/eng/news/2005+01+01.htm` as input, I get `yes` as output.
MarcoS
It works because the `.` *does* include the `+` character. If you change `.` to `[^+]` in your example expression, it wont work (because there are two `+` characters, and only one tested for/allowed).
Peter Boughton
The only difference between `.*` and `.*\+.*` is that the latter *must* include at least one `+` whilst the former can include anything (except newlines, which shouldn't apply with URL rewriting anyway).
Peter Boughton
@Peter Boughton: I see your point, and you're right. However, I understood that the requirement was to match a URLs including at least one `+`, and that's why I wrote the regex in that way.
MarcoS
We'll need to wait for the OP to return and clarify that then.
Peter Boughton