UrlRewrite正则表达式匹配、配置实例和分析

前端之家收集整理的这篇文章主要介绍了UrlRewrite正则表达式匹配、配置实例和分析前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

自己对正则表达式不熟悉,所以在配置UrlRewrite的时候遇到了很大的问题,终于在网上找到一些资料,基本上可以解决常用的配置:

  1. urlrewrite.xml配置:
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE urlrewrite SYSTEM "../resources/urlrewrite3.2.dtd" >
  3. <urlrewrite>
  4. <rule>
  5. <note>
  6. 无参数匹配
  7. </note>
  8. <from>^/([_a-zA-Z]+[_0-9a-zA-Z-/]*[_0-9a-zA-Z]+)$</from>
  9. <to type="forward">/$1.do</to>
  10. </rule>
  11. <rule>
  12. <note>
  13. 将所有.jsp文件映射为.html
  14. </note>
  15. <from>^/([_a-zA-Z]+[_0-9a-zA-Z-/]*[_0-9a-zA-Z]+).html$</from>
  16. <to type="forward">/$1.jsp</to>
  17. </rule>
  18. <!--
  19. <outbound-rule>
  20. <note>
  21. The outbound-rule specifies that when response.encodeURL is called (if you are using JSTL c:url)
  22. the url /rewrite-status will be rewritten to /test/status/.
  23. The above rule and this outbound-rule means that end users should never see the
  24. url /rewrite-status only /test/status/ both in thier location bar and in hyperlinks
  25. in your pages.
  26. </note>
  27. <from>/rewrite-status</from>
  28. <to>/test/status/</to>
  29. </outbound-rule>

EXAMPLES
  1. Redirect one url
  2. <rule>
  3. <from>/some/old/page.html</from>
  4. <to type="redirect">/very/new/page.html</to>
  5. </rule>
  6. Redirect a directory
  7. <rule>
  8. <from>/some/olddir/(.*)</from>
  9. <to type="redirect">/very/newdir/$1</to>
  10. </rule>
  11. Clean a url
  12. <rule>
  13. <from>/products/([0-9]+)</from>
  14. <to>/products/index.jsp?product_id=$1</to>
  15. </rule>
  16. eg,/products/1234 will be passed on to /products/index.jsp?product_id=1234 without the user noticing.
  17. Browser detection
  18. <rule>
  19. <condition name="user-agent">Mozilla/[1-4]</condition>
  20. <from>/some/page.html</from>
  21. <to>/some/page-for-old-browsers.html</to>
  22. </rule>
  23. eg,will pass the request for /some/page.html on to /some/page-for-old-browsers.html only for older
  24. browsers whose user agent srtings match Mozilla/1,Mozilla/2,Mozilla/3 or Mozilla/4.
  25. Centralised browser detection
  26. <rule>
  27. <condition name="user-agent">Mozilla/[1-4]</condition>
  28. <set type="request" name="browser">moz</set>
  29. </rule>
  30. eg,all requests will be checked against the condition and if matched
  31. request.setAttribute("browser","moz") will be called.
  32. -->
  33. </urlrewrite>
  34.  
  1. <rule>
  2. <note>homepage blog username like http://blog.csdn.net/pigo</note>
  3. <from>/u/([a-zA-Z0-9 ]+)/?$</from>
  4. <to>/user/main.jsp?username=$1 </to>
  5. </rule>
  6. <rule>
  7. <note>homepage blog username like http://blog.csdn.net/pigo/RSS.xml </note>
  8. <from>/u/([a-zA-Z0-9 ]+)/RSS.xml $</from>
  9. <to>/user/RSS.jsp?username=$1 </to>
  10. </rule>
  11. <rule>
  12. <note> blog catgory http://blog.csdn.net/pigo/category/42406.html</note>
  13. <from>/u/([a-zA-Z0-9 ]+)/category/([a-zA-Z0- 9 ]+).html</from>
  14. <to>/user/cat.jsp?username=$1 &catno=$ 2 </to>
  15. </rule>
  16. <rule>
  17. <note> blog catgory RSS http://blog.csdn.net/pigo/category/42406.xml </note>
  18. <from>/u/([a-zA-Z0-9 ]+)/category/([a-zA-Z0- 9 ]+).xml </from>
  19. <to>/user/catRSS.jsp?username=$1 &catno=$ 2 </to>
  20. </rule>
  21. <rule>
  22. <note> blog photo catgory http://blog.csdn.net/pigo/photo/42406.html</note>
  23. <from>/u/([a-zA-Z0-9 ]+)/photo/([a-zA-Z0- 9 ]+).html</from>
  24. <to>/user/photo.jsp?username=$1 &catno=$ 2 </to>
  25. </rule>
  26. <rule>
  27. <note>blog issue http://blog.csdn.net/pigo/archive/2006/03/23/783904.html</note>
  28. <from>/u/([a-zA-Z0-9 ]+)/archive/([ 0 - 9 ]+)/([ 0 - 9 ]+)/([ 0 - 9 ]+)/([a-zA-Z0- 9 ]+).html</from>
  29. <to>/user/issue.jsp?username=$1 &blogno=$ 5 </to>
  30. </rule>
  31. <rule>
  32. <note>blogarchive by month http://blog.csdn.net/pigo/archive/2005/08.html </note>
  33. <from>/u/([a-zA-Z0-9 ]+)/archive/([ 0 - 9 ]+)/([ 0 - 9 ]+).html</from>
  34. <to>/user/archivelist.jsp?username=$1 &year=$ 2 &month=$ 3 </to>
  35. </rule>
  36. <rule>
  37. <note>blogapi by user http://blog.csdn.net/pigo/blogapi.html </note>
  38. <from>/u/([a-zA-Z0-9 ]+)/blogapi.html</from>
  39. <to>/blogapi/?username=$1 </to>
  40. </rule>
  41. <rule>
  42. <note>blogadmin by user http://blog.csdn.net/pigo/blogadmin.html </note>
  43. <from>/u/([a-zA-Z0-9 ]+)/blogadmin.html</from>
  44. <to>/admin/index.jsp?username=$1 </to>
  45. </rule>


^[A-Za-z]+$ //匹配由26个英文字母组成的字符串
^[A-Z]+$ //匹配由26个英文字母的大写组成的字符串
^[a-z]+$ //匹配由26个英文字母的小写组成的字符串
^[A-Za-z0-9]+$ //匹配由数字和26个英文字母组成的字符串
^/w+$ //匹配由数字、26个英文字母或者下划线组成的字符串

实际上在ISAPI Rewrite中使用最多的两个正则表达式就是代表匹配字幕和数字的([a-z0-9/_]+)以及代表数字的([0-9]+),只要灵活的掌握这两点,并且按照上面的公式去套用的话,正则表达式并不是一件难办的事。

猜你在找的正则表达式相关文章