urlrewriter的使用以及出现的问题

来源:互联网 发布:哪个软件有腾落指标 编辑:程序博客网 时间:2024/05/21 08:54

 最近开始在用urlrewriter ,记录一下使用情况以及出现的各种问题

首先要明确的是为什么要使用url:主要考虑seo以及用户体验,怎么讲呢。如果你的页面上全是.do链接然后form也使用post的话,用户不能将网页添加到收藏夹,同时你的网页也不能被搜索引擎发现,其实这个才是最主要的。当然你经过重写的url也屏蔽你的后台业务逻辑。

其实使用是很简单的,在你的web应用中使用url重写其实经过很简单的配置就可以实现的

首先去http://tuckey.org/urlrewrite/ 下载urlrewritefilter-3.1.0.zip  

我使用的是beta版 版本应该没多大问题的

其实里面只有两个文件,一个是urlrewrite-3.1.0.jar,你在使用的时候只要将这个jar文件放到你的classpath中就可以使用urlrewriterl

另外一个配置文件urlrewrite.xml如下

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 3.1//EN"
  3.         "http://tuckey.org/res/dtds/urlrewrite3.1.dtd">
  4. <!--
  5.     Configuration file for UrlRewriteFilter
  6.     http://tuckey.org/urlrewrite/
  7. -->
  8. <urlrewrite>
  9.     <rule>
  10.         <note>
  11.             The rule means that requests to /test/status/ will be redirected to /rewrite-status
  12.             the url will be rewritten.
  13.         </note>
  14.         <from>/test/status/</from>
  15.         <to type="redirect">%{context-path}/rewrite-status</to>
  16.     </rule>
  17.     <outbound-rule>
  18.         <note>
  19.             The outbound-rule specifies that when response.encodeURL is called (if you are using JSTL c:url)
  20.             the url /rewrite-status will be rewritten to /test/status/.
  21.             The above rule and this outbound-rule means that end users should never see the
  22.             url /rewrite-status only /test/status/ both in thier location bar and in hyperlinks
  23.             in your pages.
  24.         </note>
  25.         <from>/rewrite-status</from>
  26.         <to>/test/status/</to>
  27.     </outbound-rule>
  28.     <!--
  29.     INSTALLATION
  30.         in your web.xml add...
  31.         <filter>
  32.             <filter-name>UrlRewriteFilter</filter-name>
  33.             <filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
  34.             <init-param>
  35.                 <param-name>logLevel</param-name>
  36.                 <param-value>WARN</param-value>
  37.             </init-param>
  38.         </filter>
  39.         <filter-mapping>
  40.             <filter-name>UrlRewriteFilter</filter-name>
  41.             <url-pattern>/*</url-pattern>
  42.         </filter-mapping>
  43.      EXAMPLES
  44.      Redirect one url
  45.         <rule>
  46.             <from>/some/old/page.html</from>
  47.             <to type="redirect">/very/new/page.html</to>
  48.         </rule>
  49.     Redirect a directory
  50.         <rule>
  51.             <from>/some/olddir/(.*)</from>
  52.             <to type="redirect">/very/newdir/$1</to>
  53.         </rule>
  54.     Clean a url
  55.         <rule>
  56.             <from>/products/([0-9]+)</from>
  57.             <to>/products/index.jsp?product_id=$1</to>
  58.         </rule>
  59.     eg, /products/1234 will be passed on to /products/index.jsp?product_id=1234 without the user noticing.
  60.     Browser detection
  61.         <rule>
  62.             <condition name="user-agent">Mozilla/[1-4]</condition>
  63.             <from>/some/page.html</from>
  64.             <to>/some/page-for-old-browsers.html</to>
  65.         </rule>
  66.     eg, will pass the request for /some/page.html on to /some/page-for-old-browsers.html only for older
  67.     browsers whose user agent srtings match Mozilla/1, Mozilla/2, Mozilla/3 or Mozilla/4.
  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. </urlrewrite>

如果说只是用用的话,按照上面的步骤加到web应用程序就可以轻松使用它强大的功能了,其实强大的还是正则表达式

 

在使用过程中遇到的问题,下次再写。主要是中文url的处理,已经一些正则表达式的使用,也没多少东西