gpt4 book ai didi

java - 为 tomcat 重写 URL

转载 作者:行者123 更新时间:2023-11-28 23:51:58 26 4
gpt4 key购买 nike

我们已经通过 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>[^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}"))

关于java - 为 tomcat 重写 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7385449/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com