2

I have configured Tuckey URL Rewriting. I have included dependency in pom.xml:

 <dependency>
            <groupId>org.tuckey</groupId>
            <artifactId>urlrewritefilter</artifactId>
            <version>4.0.4</version>
        </dependency>

added filter in web.xml:

    <filter>
        <filter-name>UrlRewriteFilter</filter-name>
        <filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
        <init-param>
            <param-name>logLevel</param-name>
            <param-value>commons</param-value>
        </init-param>
        <init-param>
            <param-name>confReloadCheckInterval</param-name>
            <param-value>60</param-value>
        </init-param>
    </filter> 
    <filter-mapping>
        <filter-name>UrlRewriteFilter</filter-name>
        <url-pattern>/*</url-pattern>
        <dispatcher>REQUEST</dispatcher>
        <dispatcher>FORWARD</dispatcher>
    </filter-mapping>    
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

created urlrewriter.xml under WEB-INF folder

  <?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 3.0//EN"
        "http://www.tuckey.org/res/dtds/urlrewrite3.0.dtd">

<urlrewrite default-match-type="wildcard">
    <!-- Struts -->
    <rule match-type="regex">
        <from>^/Profile/([0-9]+)$</from>
        <to>/Profile?id=$1</to>
    </rule>   
 
    <!-- Remove JSESSIONID from URLs when cookies disabled -->
    <!-- http://stackoverflow.com/questions/962729/is-it-possible-to-disable-jsessionid-in-tomcat-servlet -->
    <outbound-rule encodefirst="true" match-type="regex">
        <name>Strip URL Session ID's</name>
        <from>^(.*?)(?:\;jsessionid=[^\?#]*)?(\?[^#]*)?(#.*)?$</from>
        <to>$1$2$3</to>
    </outbound-rule>
</urlrewrite>

Now from browser I am requesting http://localhost:8080/Test/Profile/123

This in not redirection to http://localhost:8080/Test/Profile?id=123

How to make this working?

EDIT:

At debug I am getting following result at Tomcat log

24-Jan-2018 17:06:45.588 INFO [http-nio-8084-exec-163] org.apache.catalina.core.ApplicationContext.log org.tuckey.web.filters.urlrewrite.UrlRewriteFilter DEBUG: starting conf reload check
24-Jan-2018 17:06:45.589 INFO [http-nio-8084-exec-163] org.apache.catalina.core.ApplicationContext.log org.tuckey.web.filters.urlrewrite.UrlRewriteFilter DEBUG: conf is not modified
24-Jan-2018 17:06:45.589 INFO [http-nio-8084-exec-163] org.apache.catalina.core.ApplicationContext.log org.tuckey.web.filters.urlrewrite.utils.ServerNameMatcher DEBUG: looking for hostname match on current server name localhost
24-Jan-2018 17:06:45.589 INFO [http-nio-8084-exec-163] org.apache.catalina.core.ApplicationContext.log org.tuckey.web.filters.urlrewrite.UrlRewriteFilter DEBUG: checking for status path on /test/Profile/2345
24-Jan-2018 17:06:45.589 INFO [http-nio-8084-exec-163] org.apache.catalina.core.ApplicationContext.log org.tuckey.web.filters.urlrewrite.UrlRewriter DEBUG: processing request for /Profile/2345
24-Jan-2018 17:06:45.590 INFO [http-nio-8084-exec-163] org.apache.catalina.core.ApplicationContext.log org.tuckey.web.filters.urlrewrite.RuleBase DEBUG: Rule 0 run called with /Profile/2345
24-Jan-2018 17:06:45.590 INFO [http-nio-8084-exec-163] org.apache.catalina.core.ApplicationContext.log org.tuckey.web.filters.urlrewrite.RuleBase DEBUG: matched "from"
24-Jan-2018 17:06:45.590 INFO [http-nio-8084-exec-163] org.apache.catalina.core.ApplicationContext.log org.tuckey.web.filters.urlrewrite.substitution.MatcherReplacer DEBUG: found 1
24-Jan-2018 17:06:45.590 INFO [http-nio-8084-exec-163] org.apache.catalina.core.ApplicationContext.log org.tuckey.web.filters.urlrewrite.substitution.MatcherReplacer DEBUG: replaced sb is /Profile?id=2345
24-Jan-2018 17:06:45.590 INFO [http-nio-8084-exec-163] org.apache.catalina.core.ApplicationContext.log org.tuckey.web.filters.urlrewrite.RuleExecutionOutput DEBUG: needs to be forwarded to /Profile?id=2345
24-Jan-2018 17:06:45.590 INFO [http-nio-8084-exec-163] org.apache.catalina.core.ApplicationContext.log org.tuckey.web.filters.urlrewrite.UrlRewriter DEBUG: rule is last
24-Jan-2018 17:06:45.590 INFO [http-nio-8084-exec-163] org.apache.catalina.core.ApplicationContext.log org.tuckey.web.filters.urlrewrite.UrlRewriteFilter DEBUG: starting conf reload check
24-Jan-2018 17:06:45.590 INFO [http-nio-8084-exec-163] org.apache.catalina.core.ApplicationContext.log org.tuckey.web.filters.urlrewrite.UrlRewriteFilter DEBUG: conf is not modified
24-Jan-2018 17:06:45.591 INFO [http-nio-8084-exec-163] org.apache.catalina.core.ApplicationContext.log org.tuckey.web.filters.urlrewrite.utils.ServerNameMatcher DEBUG: looking for hostname match on current server name localhost
24-Jan-2018 17:06:45.591 INFO [http-nio-8084-exec-163] org.apache.catalina.core.ApplicationContext.log org.tuckey.web.filters.urlrewrite.UrlRewriteFilter DEBUG: checking for status path on /test/Profile
24-Jan-2018 17:06:45.591 INFO [http-nio-8084-exec-163] org.apache.catalina.core.ApplicationContext.log org.tuckey.web.filters.urlrewrite.UrlRewriter DEBUG: processing request for /Profile
24-Jan-2018 17:06:45.591 INFO [http-nio-8084-exec-163] org.apache.catalina.core.ApplicationContext.log org.tuckey.web.filters.urlrewrite.RuleBase DEBUG: Rule 0 run called with /Profile

Tomcat console is showing:

 MonitorFilter::WARNING: the monitor filter must be the first filter in the chain.
Roman C
  • 49,761
  • 33
  • 66
  • 176
xrcwrn
  • 5,339
  • 17
  • 68
  • 129

1 Answers1

1

You can rewrite any URL if you place Tuckey URL rewrite filter before the Struts2 filter in the web application deployment descriptor. Use this guide: Tuckey URLRewrite How-To.


Also you can use URL rewrite mod for Apache Web Server in front of your servlet container. Use this guide: URL Rewriting for Beginners.

Roman C
  • 49,761
  • 33
  • 66
  • 176
  • tuckey filter is before the struts2 filter I have updted the web.xml in in question – xrcwrn Jan 24 '18 at 08:36
  • Read the guide, debug, after that tell us the problem. – Roman C Jan 25 '18 at 00:04
  • Requesting url is matching and it is replaced with forwarded url but my action class is not get called – xrcwrn Jan 25 '18 at 09:11
  • It is because the request is forwarded to wrong URL and action mapper can't find mapping for that URL. You should read the guide if you want the filter to forward to the action with the correct regex. – Roman C Jan 25 '18 at 19:29
  • I am following the document http://tuckey.org/urlrewrite/manual/3.0/guide.html from there i got this example ` ^/products/([0-9]+)$ /products/index.jsp?product_id=$1 ` /products/1234 will be passed on to /products/index.jsp?product_id=1234 . I am unable to find where I an wrong – xrcwrn Jan 28 '18 at 07:17
  • The rule is wrong. However the one from the question should forward to another URL for which you didn't post mapping. – Roman C Jan 28 '18 at 22:52
  • I tried but didnt get any answer for this – xrcwrn Feb 17 '18 at 06:27
  • Strange, it worked fine for me. – Roman C Feb 17 '18 at 13:26