最新消息:

用UrlRewriteFilter实现不同WebContext间的url rewrite

技术 admin 2586浏览 0评论

    在新平台中,由于需要保持与原有CP、SP的接口的不变(目前的接口都为基于http协议的),例如原来与CP的接口为:

http://www.yeeach.com/platform/retail.do?act=verifyIp。

为了保证系统平滑的迁移,老的平台需要保留一段时间,新的平台需要以新的Webcontext方式进行部署,例如新的应用的web路径为:

http://www.yeeach.com/platformnew,与cp的接口假定新平台的处理接口为http://www.yeeach.com/platformnew/ retail.action?act=verifyIp,也即需要进行如下的迁移处理:

http://www.yeeach.com/platform/retail.do?act=verifyIp        –>         http://www.yeeach.com/platformnew/retail.action?act=verifyIp

    此种情况下与为了搜索引擎优化而进行的URL优化(也即现在流行的所谓的REST)将

http://www.yeeach.com/platform/retail.do/act/verifyIP –> http://www.yeeach.com/platform/retail.do?act=verifyIp

的场景并不相同

    同时由于跨了不同的Web Context,因此采用strtus2的struts.action.extension=action,do来实现action不同后缀的方式也行不通。

    可行的解决方案有两种:

1、  利用前端的apache或lighttpd的mod_rewrite来实现

2、  利用java开源项目UrlRewriteFilter http://tuckey.org/urlrewrite/来实现类似mod_rewrite的功能

    由于目前尚未有太多的静态页面内容,因此尚未部署apache或lighttpd(做门户时候再基于架设lighttpd),同时考虑到相对独立性,因此先采用纯java的UrlRewriteFilter解决方案来实现,简单描述一下实现方法:

1、  下载及安装UrlRewriteFilter

http://urlrewritefilter.googlecode.com/files/urlrewritefilter-3.1.0.zip 下载 UrlRewriteFilter 3.1.0版本

2、将urlrewrite-3.1.0.jar部署到旧的platform/WEB-INF/lib下

3、修改web.xml,增加如下内容

        <filter>

            <filter-name>UrlRewriteFilter</filter-name>

            <filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>

            <init-param>

                <param-name>logLevel</param-name>

                <param-value>WARN</param-value>

            </init-param>

        </filter>

        <filter-mapping>

            <filter-name>UrlRewriteFilter</filter-name>

            <url-pattern>/*</url-pattern>

        </filter-mapping>

4、在旧的webapp platform中部署urlrewrite.xml到WEB-INF/下,内容如下

<?xml version=”1.0″ encoding=”utf-8″?>

<!DOCTYPE urlrewrite PUBLIC “-//tuckey.org//DTD UrlRewrite 3.1//EN”

        “http://tuckey.org/res/dtds/urlrewrite3.1.dtd”>

<urlrewrite use-query-string=”true”>

    <rule>

        <from>^/retail.do\?act=(.*)$</from>

        <to type=”forward”>/platformnew/retail.action?act=$1</to>

    </rule>

</urlrewrite>

注意事项:

1)、实例的请求形式为:

http://www.yeeach.com/platform/retail.do?act=verifyIp         –>         http://www.yeeach.com/platformnew/retail.action?act=verifyIp

2)、对于to type可以为redirect、forward等,由于forward不能跨不同context之间,因此只能采用redirect方式。对于在统一Webcontext请求可以采用forward方式

3)、from和to都支持类似于apache mod_rewrite正则表达式用法,具体参看文档

4)、对于urlrewritefilter 3.x版本,对于诸如/retail.action?act=verifyIp这样的参数字符串,缺省情况下urlrewritefilter并不处理,由参数use-query-string决定,缺省为false。需要显性设置为true:<urlrewrite use-query-string=”true”>

摘自:http://urlrewritefilter.googlecode.com/svn/trunk/src/doc/manual/3.1/index.html

use-query-string    (optional) 

  false (default)       The query string will not be appended to the url that the “from” element matches against.

  true                     The query string will be appended to the url that the “from” element matches against.

 

5)、一定要注意在from 的查询url中的?需要进行转义处理:\?,不然会报错。因为?字符在正则表达式是0或1多个字符。to中不需要转义

6)、对url中&字符在xml文件中,用:&amp;

 

转载请注明:出家如初,成佛有余 » 用UrlRewriteFilter实现不同WebContext间的url rewrite

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址