Search code examples
javahttp-redirectspring-mvcurl-rewritingtuckey-urlrewrite-filter

How to permanently redirect JSP with parameters using urlRewriteFilter?


We are moving old JSP based web application to Spring MVC controller and using urlRewriteFilter (http://tuckey.org/urlrewrite/) for redirects.

We need to permanently redirect old JSP with parameters to controller view:

FROM: /products/product-detail.jsp?productId=666
TO: /product-detail?id=666

Important thing here is to use type="permanent-redirect" as we need the redirect to be SEO friendly.

I have seen many examples and posts of using urlRewriteFilter, all dealing with rewriting of plain JSP without parameters e.g. some.jsp to /newurl

So far only achievement is using forward:

<rule>
    <name>Product Detail Old</name>
    <from>^/products/product-detail.jsp(.*)$</from>
    <to type="forward">/product-detail</to>
</rule>

But this of course does not rewrite the URL at all:

it results in: /products/product-detail.jsp?productId=666

We have tried also this but it does not work:

<rule>
    <from>/products/product-detail.jsp?(.+)</from>
    <to type="permanent-redirect">/product-detail?$1</to>
</rule>

it results in /product-detail?p

Has anyone could help to construct a rule which will satisfy the above criteria using urlRewriteFilter?

Help most appreciated.


Solution

  • Would the following work?

    <rule>
        <from>/products/product-detail.jsp\?productId=([0-9])</from>
        <to type="permanent-redirect">/product-detail?id=$1</to>
    </rule>
    

    EDIT

    Sorry. Forgot the slash above in front of the question mark, which is a reserved character within the regex, unless you escape it.

    This caught me out yesterday actually, mainly down to copying directly from an old mod_rewrite rule we had in place.