2011-09-12 113 views
0

我们已经通过tomcat rewrite.xml文件为java应用程序定义了url规则。对于tomcat的Url重写

<rule> 
    <condition name="host" operator="equal">example-laboratories.pt</condition> 
    <from>[^pt]/(.*)$</from> 
    <to type="redirect">http://example-laboratories.pt/pt/$1</to> 
</rule> 

当我们请求URL http://example-laboratories.pt URL,但我需要在用户请求http://example-laboratories.pt/pt重定向到http://example-laboratories.pt/pt这工作。现在上面的规则重定向到http://example-laboratories.pt/pt/pt(注意最后的附加'pt')。

+1

我没有_quite_明白重写你想这样我就可以不建议解决方案,但肯定是一个正则表达式问题。 (来自''不符合你的期望...) –

回答

0

我不清楚自己想做的事,但它看起来就像你在你的正则表达式中有一个错误:

<from>[^pt]/(.*)$</from> 

这实际上说,“除了‘P’或‘T’的任何字符,然后是斜线,然后是零或更多。“

我不确定那是你想要的,但它看起来不像你想要做的。如果使用不同的URL重写工具,则可以避免大部分正则表达式。 (它看起来像您正在使用Tuckey?)

你可以做到这一点使用OCPsoft重写(http://ocpsoft.com/rewrite/

例如:

ConfigurationBuilder.begin() 
.defineRule() 
.when(Domain.matches("example-laboratories.pt") 
    .and(Path.matches("/{1}/{2}") 
     .where("1").matches("(?!pt)[^/]+") 
     .where("2").matches(".*"))) 
.perform(Redirect.permanent(context.getContextPath() + "/pt/{2}"))