【问题标题】:Url rewrite for tomcat为 tomcat 重写 URL
【发布时间】:2011-10-27 13:17:52
【问题描述】:

我们已经通过tomcat rewrite.xml文件为java应用定义了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')。

【问题讨论】:

  • 我没有完全了解您想要什么重写,所以我无法提出解决方案,但这肯定是正则表达式问题。 (from 与您的预期不符……)

标签: java regex tomcat url-rewriting


【解决方案1】:

我不太确定你想做什么,但看起来你的正则表达式有错误:

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

这实际上是说“除 'p' 或 't' 之外的任何字符,后跟一个斜杠,然后是零个或多个任何字符。”

我不确定这是否是您想要的,但这似乎不是您想要做的。如果您使用不同的 URL 重写工具,您可以在很大程度上避免使用正则表达式。 (看起来你在使用 Tuckey?)

您可以使用 OCPsoft Rewrite (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}"))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-04-30
    • 2015-10-02
    • 2014-11-10
    • 2011-10-01
    • 2016-04-09
    • 2012-01-19
    • 2021-10-11
    • 2010-12-06
    相关资源
    最近更新 更多