java - 为 tomcat 重写 URL

标签 java regex tomcat url-rewriting

我们已经通过 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/

相关文章:

java - 如何在java中将聚合运算符的输出存储为字符串?

regex - 如何获取包含特定短语的 XML 文件的数量?

hibernate - java.lang.VerifyError : class org. hibernate.type.WrappedMaterializedBlobType 覆盖 final方法 getReturnedClass.()Ljava/lang/Class;

java - 将 Arraylist 中的对象替换为 Arraylist 中另一个预先存在的对象

java - 尝试从 Java 中的控制台读取

sql - 获取Sql Server中以某个字母开头的按字母顺序排列的记录

java - 如何查找包含非单词字符的字符串?

tomcat - Tomcat实例上的多个SSL端口

Javascript 文件未更新(第一次)

java - 正则表达式replaceAll不适用于速度模板