---
title: "URLRewriter实现伪静态"
author: "haige18"
date: "2010-10-27T21:53:10Z"
tags: ["Blog", "HTML", "JSP", "Web", "XML"]
categories: ["企业架构"]
slug: "urlrewriter"
draft: false
lastmod: "2010-10-27T21:53:10Z"
id: 795964
url: "https://haige18.iteye.com/blog/795964"
---

**URLREwriter组件**

下载地址:[http://www.chx99.cn/file/URLRewriter.rar](http://www.chx99.cn/file/URLRewriter.rar)

 

实现url重写的步骤：

首先，通过上述的网址将**URLREwriter组件**下载到本地，放到项目下的/WEB-INF/lib目录下

再从网上找到如下的配置文件urlrewrite.xml，如下：（放在/WEB-INF目录下）

&lt;?xml version="1.0" encoding="utf-8"?&gt;  
&lt;!DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 2.6//EN"  
        "[http://tuckey.org/res/dtds/urlrewrite2.6.dtd](http://tuckey.org/res/dtds/urlrewrite2.6.dtd)"&gt;

&lt;!--

    Configuration file for UrlRewriteFilter  
    [http://tuckey.org/urlrewrite/](http://tuckey.org/urlrewrite/)

--&gt;  
&lt;urlrewrite&gt;

    &lt;rule&gt;  
        &lt;note&gt;  
            The rule means that requests to /test/status/ will be redirected to /rewrite-status  
            the url will be rewritten.  
        &lt;/note&gt;  
        &lt;from&gt;/test/status/&lt;/from&gt;  
        &lt;to type="redirect"&gt;%{context-path}/rewrite-status&lt;/to&gt;  
    &lt;/rule&gt;

    &lt;outbound-rule&gt;  
        &lt;note&gt;  
            The outbound-rule specifies that when response.encodeURL is called (if you are using JSTL c:url)  
            the url /rewrite-status will be rewritten to /test/status/.

            The above rule and this outbound-rule means that end users should never see the  
            url /rewrite-status only /test/status/ both in thier location bar and in hyperlinks  
            in your pages.  
        &lt;/note&gt;  
        &lt;from&gt;/rewrite-status&lt;/from&gt;  
        &lt;to&gt;/test/status/&lt;/to&gt;  
    &lt;/outbound-rule&gt;

    &lt;!--

    INSTALLATION

        in your web.xml add...

        &lt;filter&gt;  
            &lt;filter-name&gt;UrlRewriteFilter&lt;/filter-name&gt;  
            &lt;filter-class&gt;org.tuckey.web.filters.urlrewrite.UrlRewriteFilter&lt;/filter-class&gt;  
            &lt;init-param&gt;  
                &lt;param-name&gt;logLevel&lt;/param-name&gt;  
                &lt;param-value&gt;WARN&lt;/param-value&gt;  
            &lt;/init-param&gt;  
        &lt;/filter&gt;  
        &lt;filter-mapping&gt;  
            &lt;filter-name&gt;UrlRewriteFilter&lt;/filter-name&gt;  
            &lt;url-pattern&gt;/\*&lt;/url-pattern&gt;  
        &lt;/filter-mapping&gt;

     EXAMPLES

     Redirect one url  
        &lt;rule&gt;  
            &lt;from&gt;/some/old/page.html&lt;/from&gt;  
            &lt;to type="redirect"&gt;/very/new/page.html&lt;/to&gt;  
        &lt;/rule&gt;

    Redirect a directory  
        &lt;rule&gt;  
            &lt;from&gt;/some/olddir/(.\*)&lt;/from&gt;  
            &lt;to type="redirect"&gt;/very/newdir/$1&lt;/to&gt;  
        &lt;/rule&gt;

    Clean a url  
        &lt;rule&gt;  
            &lt;from&gt;/products/(\[0-9]+)&lt;/from&gt;  
            &lt;to&gt;/products/index.jsp?product\_id=$1&lt;/to&gt;  
        &lt;/rule&gt;  
    eg, /products/1234 will be passed on to /products/index.jsp?product\_id=1234 without the user noticing.

    Browser detection  
        &lt;rule&gt;  
            &lt;condition name="user-agent"&gt;Mozilla/\[1-4]&lt;/condition&gt;  
            &lt;from&gt;/some/page.html&lt;/from&gt;  
            &lt;to&gt;/some/page-for-old-browsers.html&lt;/to&gt;  
        &lt;/rule&gt;  
    eg, will pass the request for /some/page.html on to /some/page-for-old-browsers.html only for older  
    browsers whose user agent srtings match Mozilla/1, Mozilla/2, Mozilla/3 or Mozilla/4.

    --&gt;

&lt;/urlrewrite&gt;

接下类就可以进行伪静态的实现了：

在上述的配置文件中加入下面代码：（缩减版）

&lt;?xml version="1.0" encoding="utf-8"?&gt;  
&lt;!DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 2.6//EN"  
        "[http://tuckey.org/res/dtds/urlrewrite2.6.dtd](http://tuckey.org/res/dtds/urlrewrite2.6.dtd)"&gt;

&lt;!--

    Configuration file for UrlRewriteFilter  
    [http://tuckey.org/urlrewrite/](http://tuckey.org/urlrewrite/)

--&gt;  
&lt;urlrewrite&gt;

    &lt;rule&gt;  
        &lt;note&gt;  
            The rule means that requests to /test/status/ will be redirected to /rewrite-status  
            the url will be rewritten.  
        &lt;/note&gt;  
        &lt;from&gt;/test/status/&lt;/from&gt;  
        &lt;to type="redirect"&gt;%{context-path}/rewrite-status&lt;/to&gt;  
    &lt;/rule&gt;  
     
        **&lt;rule&gt;**  
            **&lt;from&gt;/student.html/(\[a-z]+)&lt;/from&gt;**  
            **&lt;to&gt;/student.do?cmd=$1&lt;/to&gt;**  
        **&lt;/rule&gt;**  
        **&lt;rule&gt;**  
            **&lt;from&gt;/student2.html/(\[a-z]+)\_(\[0-9]+)&lt;/from&gt;**  
            **&lt;to&gt;/student.do?cmd=$1&amp;id=$2&lt;/to&gt;**  
        **&lt;/rule&gt;**

          
    &lt;outbound-rule&gt;  
        &lt;note&gt;  
            The outbound-rule specifies that when response.encodeURL is called (if you are using JSTL c:url)  
            the url /rewrite-status will be rewritten to /test/status/.

            The above rule and this outbound-rule means that end users should never see the  
            url /rewrite-status only /test/status/ both in thier location bar and in hyperlinks  
            in your pages.  
        &lt;/note&gt;  
        &lt;from&gt;/rewrite-status&lt;/from&gt;  
        &lt;to&gt;/test/status/&lt;/to&gt;  
    &lt;/outbound-rule&gt;

&lt;/urlrewrite&gt;

 

最后在你的项目中就可以将路径写成伪静态的样子，如：

&lt;a href="**student.html/list**"&gt;查看学生列表&lt;/a&gt;

通过urlrewrite.xml的解析

**&lt;rule&gt;**  
            **&lt;from&gt;/student.html/(\[a-z]+)&lt;/from&gt;**  
            **&lt;to&gt;/student.do?cmd=$1&lt;/to&gt;**

  **&lt;/rule&gt;**

就将该路径转化为：

**student.do?cmd=list**      (这是真实的请求路径)

 

 

又如：

&lt;a href="**student2.html/loadedit\_2**"&gt;查看学生列表&lt;/a&gt;

通过urlrewrite.xml的解析

**&lt;rule&gt;**  
            **&lt;from&gt;/student2.html/(\[a-z]+)\_(\[0-9]+)&lt;/from&gt;**  
            **&lt;to&gt;/student.do?cmd=$1&amp;id=$2&lt;/to&gt;**  
 **&lt;/rule&gt;**

就将该路径转化为：

**student.do?cmd=loadedit&id=2**      (这是真实的请求路径)

 

 

由于伪路径经常带有“/”，会将页面路径嵌套，所以建议使用绝对路径

在jsp页面上写上：

&lt;%  
String path = request.getContextPath();  
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
%&gt;

则上面的路径可以写成：

&lt;a href="&lt;%=basePath%&gt;**student2.html/loadedit\_2**"&gt;查看学生列表&lt;/a&gt;

 

 

 

来自：[http://blog.sina.com.cn/s/blog\_6145ed810100dvs2.html](http://blog.sina.com.cn/s/blog_6145ed810100dvs2.html)