Bob
Bob

Reputation: 281

How to use UrlRewriteFilter to remove ".jsp" from a url with query string

For some reason, I need to remove ".jsp" from a Url with query string in a Spring MVC project, using UrlRewriteFilter.

For example, I want to change

http://localhost:8080/admin/project.jsp?m_product=GA&m_code1=QULQ2U

to

http://localhost:8080/admin/project?m_product=GA&m_code1=QULQ2U

I have tried various rules, but no success.

For example, when I set the rule as

<rule>
    <from>/admin/project.jsp?(.+)</from>
    <to type="redirect">/admin/project?$1</to>
</rule>

UrlRewriteFilter will change

admin/project.jsp?m_product=GA&m_code1=QULQ2U

to

admin/project?p

When I use the following rule to escape the question mark before the query string

<rule>
    <from>/admin/project.jsp\?(.+)</from>
    <to type="redirect">/admin/project?$1</to>
</rule>

UrlRewriteFilter will not rewrite the URL.

Your help is appreciated.

Upvotes: 3

Views: 8275

Answers (4)

dinu0101
dinu0101

Reputation: 499

If you want to remove the extension(.jsp) from project using Tuckey url re-writing filter then it may helpful for you.

From
    http://locatohost:8080/urlrewrite/feedback.jsp 
To
    http://locatohost:8080/urlrewrite/feedback
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 4.0//EN" 
"http://www.tuckey.org/res/dtds/urlrewrite4.0.dtd">
<urlrewrite>
  <rule>
    <from>^/?([a-z]+)$</from>
    <to>/$1.jsp</to>
  </rule>
</urlrewrite>

And if you want to vise-versa(means you want to remove .jsp from page hyperlinks)then use below rule

  <outbound-rule>
    <from>^/?([a-z]+)(.(jsp|jspx))$</from>
    <to>$1</to>
  </outbound-rule>

so that when you use

<LI><A href="complaints.htm">Complaints</A></LI>

then it will be written in page as

<LI><A href="complaints">Complaints</A></LI>

Upvotes: 4

Lincoln
Lincoln

Reputation: 3191

With the OCPsoft Rewrite | URLRewriteFilter, this would be as simple as specifying one rule:

.addRule(Join.path("/admin/project")
             .to("/admin/project.jsp")
             .withInboundCorrection());

This handles both the inbound Forward, the outbound HTML replacement, and also redirects from the old .jsp URL to the new /admin/project URL if someone asks for the old URL, or has an old bookmark.

The query-string is automatically handled for you, so no need to worry about regular expressions.

Upvotes: 2

Bob
Bob

Reputation: 281

Thanks for the helps.

The problem is Solved!

To achieve the goal, I changed the rule to

<rule>
    <from>/admin/project.jsp(.*)</from>
    <to type="forward">/project</to>
</rule>

The trick is to replace redirect to forward.

Upvotes: 2

Michael-O
Michael-O

Reputation: 18405

Try

<rule>
  <from>/admin/project\.jsp\?(.+)</from>
  <to type="redirect">/admin/project?$1</to>
</rule>

As a general rule, you should rather use a controller for such a task and let the crappy JSP do what it is supposed to do: VIEW

Upvotes: 0

Related Questions