`
somebody
  • 浏览: 80145 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

tuckey urlrewrite 3.x的问题

    博客分类:
  • java
阅读更多
昨天晚上配置myblog的rewrite。发现一个奇怪的问题。由于现在使用的这个pjblog,为了让搜索引擎收录的连接有效。我想把原来的asp连接rewrite到我的新程序上面。所以有这样一条规则。

    <rule>
        
<from>^/article.asp\?id=(.*)$<!---->from>
        
<to type="redirect">/entry/$1.jhtml<!---->to>
    
<!---->rule>
     但是我这样的连接总是匹配不到,只要去掉那个?就可以了。这个正则表达式是没有问题的。/article.asp?id=64是可以匹配的到的。
    后来看3.0的manual (http://tuckey.org/urlrewrite/manual/3.0/)才发现原来是这个的问题。

<urlrewrite> element</urlrewrite>

The top level element.

Attribute Possible Value Explanation
default-match-type
(optional)
regex (default) All rules and thier conditions will be processed using the Java Regular Expression engine (unless match-type is specified on a rule).
wildcard All rules and thier conditions will be processed using the Wildcard Expression engine (unless match-type is specified on a rule).
decode-using
(optional)
utf8 (default) When URL is decoded UTF-8 will be used.
null Do not decode.
[encoding] Any string representing a supported character encoding eg, ISO-8859-1. See Java Charset Object for more info.
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.
use-context
(optional)
false (default) The context path will not be added to the url that the "from" element matches against.
true The context path will be added to the url that the "from" element matches against.

就是那个use-query-string 的问题,默认的是不使用query-string就是把?后面的都忽略了。所以就不能匹配到了。只要在<urlrewrite>里面加一个属性就可以了。
</urlrewrite>
<urlrewrite use-query-string="true">
    
<!---->urlrewrite>

 

分享到:
评论
3 楼 somebody 2007-01-23  
我用了filter的。我debug的时候看到了得到的是中文。
等有时间看看怎么改了。
我觉得它是在forward的时候没有encode回去。
2 楼 codeutil 2007-01-23  

中文问题要改源代码才能.
其实中文问题不能怪 tuckey urlrewrite,
而是servlet容器的问题.
比如:
<from>/u/([a-zA-Z0-9\u4e00-\u9fa5]+)/?$</from>
<to>/user.jsp?username=$1</to>

使用tomcat.则可以处理中文,但是,
比如url为:
http://localhost:8083/webapp/u/%C6%D5%CD%A8%D3%C3%BB%A7
源代码UrlRewriter.java里getNewChain方法,原本使用
String originalUrl = StringUtils.trim(hsRequest.getRequestURI());
来获取原始url,
而现在不知怎么的改用
String servletPath = hsRequest.getServletPath();   // /servlet/MyServlet

String originalUrl = servletPath;

,而在上述情况中,
第一种方法得到的originalUrl 是 "/u/%C6%D5%CD%A8%D3%C3%BB%A7"
第二种得到的却不是"/u/普通用户"
而是"/u/??",因为tomcat已经按iso8859-1编码解码了,却解的不正确.
接下来第128行的,urlrewriter自己又作了一次解码,从问号解码肯定没法得到正确的信息了
  originalUrl = URLDecoder.decode(originalUrl, conf.getDecodeUsing());
             
因此导致无法处理中文.

知道这个原因,改代码就很方便了.
但是,光是改这里还不够,虽然可以正确的转换成
"/user.jsp?username=普通用户",但是接下来的forward后,
后面的 user.jsp得到的 username还会乱码的.
所以,最好的方法就是先加上过滤器确定编码,然后再根据实际情况进行修正,使之支持中文.

乱码问题是servlet容器造成的,而不是 tuckey urlrewrite的问题


1 楼 somebody 2007-01-10  
还有一个就是中文的问题。没有找到解决方法。不知道有那位能人解决了的请指教。

相关推荐

Global site tag (gtag.js) - Google Analytics