javaurlrewriter

Java URLrewrite Filter cannot find a match


I am using the URLRewrite Filter from tuckey ( http://www.tuckey.org/urlrewrite/ )

Now i created some rules, but there is one case which gives me headaches. The URL: /group/1/update should be rewritten as: /group?id=1&action=update

I created a regexp Pattern for this case: ^/group/([0-9]+)/(\w*)$, but the Filter is unable to rewrite it. There is no match.

Inside my TestCase i ran this url through all my patterns, just to check. And i found the match as i expected.

assertFalse( "/group/1/update".matches("^/group/create$") );
assertFalse( "/group/1/update".matches("^/group/save$") );
assertFalse( "/group/1/update".matches("^/group/([0-9]+)/?$") );
assertTrue( "/group/1/update".matches("^/group/([0-9]+)/(\\w*)$") );
assertFalse( "/group/1/update".matches("^/group/([0-9]+)/(\\w*)\\?(.*)+$") );

So how come the Filter is unable to find a Rule?

Just to include everything, here is my urlrewrite.xml, or part of it:

<rule>
    <from>^/group/create$</from>
    <to>/group?action=create</to>
</rule>
<rule>
    <from>^/group/save$</from>
    <to>/group?action=save</to>
</rule>
<rule>
    <from>^/group/([0-9]+)/?$</from>
    <to>/group?id=$1&amp;action=show</to>
</rule>
<rule>
    <from>^/group/([0-9]+)/(\\w*)$</from>
    <to>/group?id=$1&amp;action=$2</to>
</rule>
<rule>
   <from>^/group/([0-9]+)/(\\w*)\\?(.*)+$</from>
    <to>/group?id=$1&amp;action=$2&amp;$3</to>
</rule>

Bye

Adrian


Solution

  • You should use single backslash \ instead of double backslash \\.

    <rule>
        <from>^/group/([0-9]+)/(\w*)$</from>
        <to>/group?id=$1&amp;action=$2</to>
    </rule>
    <rule>
        <from>^/group/([0-9]+)/(\w*)\?(.*)+$</from>
        <to>/group?id=$1&amp;action=$2&amp;$3</to>
    </rule>
    

    In Java code, the double backalsh is used to represent a single backslash. Since the string is read from external file, there is no need the escape the backslash.