urlrewrite伪静态 及多参数传递-附正则表达式语法 [轉]

时间:2022-02-27 05:24:20
首先 加载 urlrewrite包

配置web。xml 
[list] 
[*]

  1. <error-page>
  2. [*]             <error-code>404</error-code>
  3. [*]             <location>/error.jsp</location>
  4. [*]     </error-page>
  5. [*]

[/list]

  1. [list]
  2. [*] <filter>
  3. [*]     <filter-name>UrlRewriteFilter</filter-name>
  4. [*]     <filter-class>
  5. [*]         org.tuckey.web.filters.urlrewrite.UrlRewriteFilter
  6. [*]     </filter-class>
  7. [*] </filter>
  8. [*] <filter-mapping>
  9. [*]     <filter-name>UrlRewriteFilter</filter-name>
  10. [*]     <url-pattern>/*</url-pattern>
  11. [*] </filter-mapping>
  12. [/list]

以及 urlrewrite.xml

  1. [list]
  2. [*] <rule>
  3. [*]
  4. [*]//或者 <from>^/test/users/([0-9]+).html$</from>
  5. [*]     <from>^/test/users/([0-9]+)(/)?$</from>  <!-- (/)? 匹配前面的字符1或0次   匹配 数字结尾的 或者数字/结尾的 11112或 11112/ -->
  6. [*]     <to>/test.jsp?users=$1</to>
  7. [*] </rule>
  8. [*]<!-- 静态 -->
  9. [*] <rule>
  10. [*]     <from>^/test.html</from>
  11. [*]     <to type="forward">/demo.jsp</to>
  12. [*] </rule>
  13. [/list]

<a href="test/users/123456">动态传参测试</a> 
<a href="test/users/123456/">动态传参测试</a> 
    <a href="test.html">静态测试</a>

多参数传递

  1. [list]
  2. [*] <rule>
  3. [*]     <from>^/test/([0-9]+)/([0-9a-zA-Z]+)$</from>  <!-- (/)? 匹配前面的字符1或0次   匹配 数字结尾的 或者数字/结尾的 11112或 11112/ -->
  4. [*]     <to type="forward">/tservert?users=$1&amp;tt=$2</to>
  5. [*] </rule>
  6. [*]
  7. [/list]
  8. <to>中 & 用&amp;[list]
  9. [*]
  10. [/list]
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <urlrewrite>
  3. <!-- 帖子列表 -->
  4. <rule>
  5. <from>^/forum-(\d+)-(\d+)\.html$</from>
  6. <to type="forward">/servlet/TopicListServelet?sid=$1&amp;page=$2</to>
  7. </rule>
  8. <!-- 帖子列表加分页
  9. <rule>
  10. <from>^/subcate_list_(\d+)\.html$</from>
  11. <to type="forward">/servlet/TopicListServelet?sid=$1</to>
  12. </rule> -->
  13. <!-- 帖子详细信息 -->
  14. <rule>
  15. <from>^/thread-(\d+)\.html$</from>
  16. <to type="forward">/servlet/TopicDetailServlet?tid=$1</to>
  17. </rule>
  18. </urlrewrite>