Question

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.

Was it helpful?

Solution 2

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.

OTHER TIPS

It does not rewrite on its own... as I have found in manual the <from> is using only the URL string without the query string. To be able to use query string we need to add <urlrewrite use-query-string="true"> After this it works like a charm! So the final rule:

<urlrewrite use-query-string="true">

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

</urlrewrite>

There are a couple of ways of dealing with this. You can use

<urlrewrite use-query-string="true">

although this will affect ALL of the rules in your file. To target the query string for a specific rule, use a condition element

<condition type="query-string">productId=([0-9])</condition>

You can then reference the query string like so

<to type="permanent-redirect">/someUrl?%{query-string}</to>

Although in your case, you're looking to change the query string so you might want to reference the captured regex

<to type="permanent-redirect">/product-detail?id=$1</to>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top