开发者

Url rewrite for tomcat

开发者 https://www.devze.com 2023-04-04 09:55 出处:网络
We have already defined url rule for java application via tomcat rewrite.xml file. <rule> <condition name=\"host\" operator=\"equal\">example-laboratories.pt</condition>

We have already defined url rule for java application via tomcat rewrite.xml file.

<rule>
    <condition name="host" operator="equal">example-laboratories.pt</condition>
    <from>[^pt]/(开发者_运维知识库.*)$</from>
    <to type="redirect">http://example-laboratories.pt/pt/$1</to>
</rule>

This works when we request the URL http://example-laboratories.pt url, but I need to redirect to http://example-laboratories.pt/pt if the user requests http://example-laboratories.pt/pt. Now the above rule redirects to http://example-laboratories.pt/pt/pt (notice the additional 'pt' on the end).


I'm not exactly sure what you want to do, but it looks like you have an error in your regular expression:

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

This actually says "any character except 'p' or 't', followed by a slash, then zero or more of anything."

I'm not sure that's what you want, but it doesn't seem like what you want to be doing. You can avoid Regular expressions for the most part if you use a different URL-rewriting tool. (It looks like you are using Tuckey?)

You can do this using OCPsoft Rewrite (http://ocpsoft.com/rewrite/)

For example:

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}"))
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号