Facing problem with UrlReWrite when using query string parameter with question mark

181 views
Skip to first unread message

jayantS

unread,
Nov 12, 2009, 8:54:22 AM11/12/09
to UrlRewrite
Following rule is not working

<outbound-rule>
<from>^/world.jsp\.?country=([a-z]+)&amp;city=([a-z]+)$</from>
<to>/world/$1/$2</to>
</outbound-rule>

but without parameter in query string works fine

<outbound-rule>
<from>^/old/url/scheme/page.jsp$</from>
<to>/tidy/page</to>
</outbound-rule>

I am doing something wrong. Please advice.

Thanks,
Jayant

Avlesh Singh

unread,
Nov 13, 2009, 1:18:09 AM11/13/09
to urlre...@googlegroups.com
Your regex looks incorrect. Special regex characters are not properly escaped. Try changing
^/world.jsp\.?country=([a-z]+)&amp;city=([a-z]+)$
to
^/world\.jsp\?country=([a-z]+)&amp;city=([a-z]+)$

Cheers
Avlesh

jayantS

unread,
Nov 13, 2009, 2:38:26 AM11/13/09
to UrlRewrite
Thanks Avlesh,

But I am still facing problem with regex provided by you.

^/world\.jsp\?country=([a-z]+)&amp;city=([a-z]+)$

Is urlrewrite App Server dependent cause I tried this on Sun Glassfish
app. server.

Thanks,
JayantS.
> > Jayant- Hide quoted text -
>
> - Show quoted text -

Avlesh Singh

unread,
Nov 13, 2009, 2:40:54 AM11/13/09
to urlre...@googlegroups.com
Is urlrewrite App Server dependent cause I tried this on Sun Glassfish app. server.
Nope. It works on all standard containers.

Copy-paste your (jsp) code snippet where this outbound rule is being used.

Cheers
Avlesh

jayantS

unread,
Nov 13, 2009, 2:51:51 AM11/13/09
to UrlRewrite
Welcome.jsp - where I used outbound rule

<html:html lang="true">
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=UTF-8">
<title><bean:message key="welcome.title"/></title>
<html:base/>
</head>
<body style="background-color: white">

<logic:notPresent name="org.apache.struts.action.MESSAGE"
scope="application">
<div style="color: red">
ERROR: Application resources not loaded -- check
servlet container
logs for error messages.
</div>
</logic:notPresent>

<h3><bean:message key="welcome.heading"/></h3>
<p><bean:message key="welcome.message"/></p>
<p><a href="world.jsp?country=usa&amp;city=nyc">Click Here</
a></p>
<p><a href="/old/url/scheme/page.jsp">some link</a></p>

</body>
</html:html>

urlrewrite.xml

<urlrewrite>
<rule enabled="true">
<from>^/world/([a-z]+)/([a-z]+)$</from>
<to>/world.jsp?country=$1&amp;city=$2</to>
</rule>

<outbound-rule>
<from>^/world\.jsp\?country=([a-z]+)&amp;city=([a-z]+)$</from>
<to>/world/$1/$2</to>
</outbound-rule>

<rule>
<from>^/tidy/page$</from>
<to>/old/url/scheme/page.jsp</to>
</rule>
<outbound-rule>
<from>^/old/url/scheme/page.jsp$</from>
<to>tidy/page</to>
</outbound-rule>
</urlrewrite>


Thanks,
JayantS
> > > - Show quoted text -- Hide quoted text -

Avlesh Singh

unread,
Nov 13, 2009, 3:35:15 AM11/13/09
to urlre...@googlegroups.com
I am not sure if this is the reason; try adding "use-query-string=true" to the root node of the urlrewrite.xml file.

Cheers
Avlesh

jayantS

unread,
Nov 13, 2009, 4:09:03 AM11/13/09
to UrlRewrite
:( I have tried with <urlrewrite use-query-string="true"> but not
works.

I am not sure but it could be only problem with ? in query string.

Thanks,
JayantS

Avlesh Singh

unread,
Nov 13, 2009, 4:11:23 AM11/13/09
to urlre...@googlegroups.com
One lame question - have you configured the filter in your web.xml file?

Cheers
Avlesh

jayantS

unread,
Nov 13, 2009, 4:43:53 AM11/13/09
to UrlRewrite
please find web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-
class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>2</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>2</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<filter>
<filter-name>UrlRewriteFilter</filter-name>
<filter-
class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-
class>
</filter>
<filter-mapping>
<filter-name>UrlRewriteFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<jsp-config>
<taglib>
<taglib-uri>/WEB-INF/struts-bean.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-bean.tld</taglib-
location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/struts-html.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-html.tld</taglib-
location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/struts-logic.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-logic.tld</taglib-
location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/struts-nested.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-nested.tld</taglib-
location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/struts-tiles.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-tiles.tld</taglib-
location>
</taglib>
</jsp-config>
</web-app>


Thanks,
JayantS

Avlesh Singh

unread,
Nov 13, 2009, 5:04:25 AM11/13/09
to urlre...@googlegroups.com
Everything looks good. It should work.

I probably spotted the error.
Your jsp has this link - <a href="world.jsp?country=usa&amp;city=nyc">Click Here</a></p>
The link is "relative" in nature, which means that if the base path for this request was - myhost.com/mycontext/index.jsp, your resultant url is myhost.com/mycontext/welcome.jsp?country... and NOT myhost.com/welcome.jsp?country...

The point being that your rule regex ^/world\.jsp\?country=([a-z]+)&amp;city=([a-z]+)$ does NOT match "world.jsp?country=usa...". Note the slash (/) at the beginning of world.jsp.. in the rule. Try removing it and see if it works.

Cheers
Avlesh

Wim De Smet

unread,
Nov 13, 2009, 5:35:10 AM11/13/09
to urlre...@googlegroups.com
Him

On Fri, Nov 13, 2009 at 8:51 AM, jayantS <sukh...@gmail.com> wrote:
> <a href="world.jsp?country=usa&amp;city=nyc">Click Here</a></p>

>> > > > <outbound-rule>
>> > > >        <from>^/world.jsp\.?country=([a-z]+)&amp;city=([a-z]+)$</from>
>> > > >        <to>/world/$1/$2</to>
>> > > > </outbound-rule>

I find it strange that this would work for the general case as you're
using just "a" which I thought doesn't call encodeURL?

But anyway, there is a problem with ampersands in outbound URLs I've
noticed in that they're sometimes encoded and sometimes not (in our
code base it's something to do with the struts tag, e.g. s:a seemingly
sometimes encoding before uwf gets the URL, sometimes not).

So what you think your outbound rule sees is:
/world.jsp?country=us&city=dc

What it might be seeing (verbatim):
/world.jsp?country=us&amp;city=dc

I would try the following rule:
<from>^/world.jsp\.?country=([a-z]+)&amp;(?:amp;)?city=([a-z]+)$</from>

Which would work for either case. Assuming your <a> tags are calling
response.encodeURL() which I'm unconvinced they are. It looks like
you're using Struts 1 in which case you might want to try using the
<html:link> tag[1] instead.

greets,
Wim

[1]:http://struts.apache.org/1.3.10/struts-taglib/tagreference.html#html:link

Rohit

unread,
Nov 13, 2009, 6:28:11 AM11/13/09
to UrlRewrite
Hi Wim,
Solution provided by you is not working, if you can get me some more
information or some piratical solution then it would be great for me.

Regards.
Rohit

On Nov 13, 3:35 pm, Wim De Smet <krom...@gmail.com> wrote:
> Him
>
> [1]:http://struts.apache.org/1.3.10/struts-taglib/tagreference.html#html:...

NextOne

unread,
Dec 2, 2009, 5:16:30 PM12/2/09
to UrlRewrite
Hi All,

did anyone figure out why it's not working with struts?

I have a application created with struts 2 using tiles.


<rule>
<from>^/test1\.html$</from>
<to type="redirect">/test2.html</to>
</rule>


the abouve rule works from the ROOT dir of the app
<a href="<s:url action="test1.html" />" > test</a>

but it tails with .action

I have a rule which redirect the http://www.../page/23432 link to
http://www.../page_view.action?id=23432

but I don't want the redirect, if I remove the redirect in <to> then I
am getting "HTTP Status 404 -"


here is the rule:
<rule>
<from>/page/([0-9]+)$</from>
<to type="redirect">%{context-path}/page_view.action?id=$1</to>
</rule>



did anyone got this urlrewrite workign with struts???


Please share you thoughts

thanks

On Nov 13, 6:28 am, Rohit <rohit.sam...@gmail.com> wrote:
> Hi Wim,
> Solution provided by you is not working, if you can get me some more
> information or some piratical solution then it would be great for me.
>
> Regards.
> Rohit
>
> On Nov 13, 3:35 pm, Wim De Smet <krom...@gmail.com> wrote:
>
>
>
> > Him
>
> > On Fri, Nov 13, 2009 at 8:51 AM, jayantS <sukhad...@gmail.com> wrote:
> > > <a href="world.jsp?country=usa&amp;city=nyc">Click Here</a></p>
> > >> > > > <outbound-rule>
> > >> > > >        <from>^/world.jsp\.?country=([a-z]+)&amp;city=([a-z]+)$</from>
> > >> > > >        <to>/world/$1/$2</to>
> > >> > > > </outbound-rule>
>
> > I find it strange that this would work for the general case as you're
> > using just "a" which I thought doesn't call encodeURL?
>
> > But anyway, there is a problem with ampersands in outbound URLs I've
> > noticed in that they're sometimes encoded and sometimes not (in our
> > code base it's something to do with thestrutstag, e.g. s:a seemingly
> > sometimes encoding before uwf gets the URL, sometimes not).
>
> > So what you think your outbound rule sees is:
> > /world.jsp?country=us&city=dc
>
> > What it might be seeing (verbatim):
> > /world.jsp?country=us&amp;city=dc
>
> > I would try the following rule:
> > <from>^/world.jsp\.?country=([a-z]+)&amp;(?:amp;)?city=([a-z]+)$</from>
>
> > Which would work for either case. Assuming your <a> tags are calling
> > response.encodeURL() which I'm unconvinced they are. It looks like
> > you're usingStruts1 in which case you might want to try using the
> > <html:link> tag[1] instead.
>
> > greets,
> > Wim
>
> > [1]:http://struts.apache.org/1.3.10/struts-taglib/tagreference.html#html:...- Hide quoted text -
Reply all
Reply to author
Forward
0 new messages