27
2016
03

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

 

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

  1. urlrewrite.xml配置:   

  2. <?xml version="1.0" encoding="UTF-8"?>   

  3. <!DOCTYPE urlrewrite SYSTEM "../resources/urlrewrite3.2.dtd" >   

  4. <urlrewrite>   

  5.          <rule>      

  6.                  <note>      

  7.                          无参数匹配     

  8.                  </note>      

  9.                  <from>^/([_a-zA-Z]+[_0-9a-zA-Z-/]*[_0-9a-zA-Z]+)$</from>      

  10.                  <to type="forward">/$1.do</to>      

  11.                 

  12.          </rule>   

  13.            

  14.          <rule>      

  15.                  <note>   

  16.                      将所有.jsp文件映射为.html   

  17.                  </note>   

  18.                  <from>^/([_a-zA-Z]+[_0-9a-zA-Z-/]*[_0-9a-zA-Z]+).html$</from>      

  19.                  <to type="forward">/$1.jsp</to>      

  20.          </rule>   

  21.            

  22.      <!--   

  23.          <outbound-rule>   

  24.                  <note>   

  25.                      The outbound-rule specifies that when response.encodeURL is called (if you are using JSTL c:url)   

  26.                      the url /rewrite-status will be rewritten to /test/status/.   

  27.            

  28.                      The above rule and this outbound-rule means that end users should never see the   

  29.                      url /rewrite-status only /test/status/ both in thier location bar and in hyperlinks   

  30.                      in your pages.   

  31.                  </note>   

  32.                  <from>/rewrite-status</from>   

  33.                  <to>/test/status/</to>   

  34.          </outbound-rule>   

  35.   

  36.   

  37.   

  38.       EXAMPLES   

  39.   

  40.       Redirect one url   

  41.          <rule>   

  42.              <from>/some/old/page.html</from>   

  43.              <to type="redirect">/very/new/page.html</to>   

  44.          </rule>   

  45.   

  46.      Redirect a directory   

  47.          <rule>   

  48.              <from>/some/olddir/(.*)</from>   

  49.              <to type="redirect">/very/newdir/$1</to>   

  50.          </rule>   

  51.   

  52.      Clean a url   

  53.          <rule>   

  54.              <from>/products/([0-9]+)</from>   

  55.              <to>/products/index.jsp?product_id=$1</to>   

  56.          </rule>   

  57.      eg, /products/1234 will be passed on to /products/index.jsp?product_id=1234 without the user noticing.   

  58.   

  59.      Browser detection   

  60.          <rule>   

  61.              <condition name="user-agent">Mozilla/[1-4]</condition>   

  62.              <from>/some/page.html</from>   

  63.              <to>/some/page-for-old-browsers.html</to>   

  64.          </rule>   

  65.      eg, will pass the request for /some/page.html on to /some/page-for-old-browsers.html only for older   

  66.      browsers whose user agent srtings match Mozilla/1, Mozilla/2, Mozilla/3 or Mozilla/4.   

  67.   

  68.      Centralised browser detection   

  69.          <rule>   

  70.              <condition name="user-agent">Mozilla/[1-4]</condition>   

  71.              <set type="request" name="browser">moz</set>   

  72.          </rule>   

  73.      eg, all requests will be checked against the condition and if matched   

  74.      request.setAttribute("browser", "moz") will be called.   

  75.   

  76.      -->   

  77.            

  78. </urlrewrite> 

  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 &amp;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 &amp;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 &amp;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 &amp;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 &amp;year=$ 2 &amp;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个英文字母或者下划线组成的字符串

     ^/d+$ //匹配由数字组成的字符串

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

 

« 上一篇 下一篇 »