Reputation: 53
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.
Upvotes: 3
Views: 2111
Reputation: 344
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>
Upvotes: 1
Reputation: 53
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>
Upvotes: 2
Reputation: 938
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.
Upvotes: 1