Google App Engine上的Catch-all URL重写(Java) [英] Catch-all URL rewrite on Google App Engine (Java)
问题描述
我正在尝试为GAE应用创建一个简短的网址,因此我使用了 UrlRewriteFilter ,但我无法正确设置它。基本上,用户得到这个:
I'm trying to create a short URL for a GAE app, so I used UrlRewriteFilter, but I can't get it set up correctly. Basically, the user is given this:
- test.com/012a-bc
以及应该重定向到的页面是
and the page that they should be redirected to is
- test.com/vote.jsp? id = 012a-bc
目前正在使用 urlrewrite.xml
这样的文件:
At the moment it's working with the urlrewrite.xml
file like this:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 4.0//EN" "http://www.tuckey.org/res/dtds/urlrewrite4.0.dtd">
<urlrewrite>
<rule>
<from>/([0-z]+)</from>
<to last="true">/vote.jsp?id=$1</to>
</rule>
</urlrewrite>
问题是所有网址现在都重定向到此,例如
The problem is that all URLs now redirect to this, so for example
- test.com/thankyou.jsp?id=0123
仍然在vote.jsp上运行页面。只有在找不到URL时,我该怎么做才能让它重定向?
still runs the page at vote.jsp. What should I do to get it to redirect only when the URL isn't found?
推荐答案
以下规则怎么办: / p>
What about the following rule:
<rule>
<from>^/([\w-]+)$</from>
<to last="true">/vote.jsp?id=$1</to>
</rule>
其中 [\w - ] +
至少有一个单词字符(字母,数字,下划线),包括 -
(短划线字符)。您使用 ^
和 $
来锚定选中文本的开头和结尾。
Where [\w-]+
is at least one any word character (letter, number, underscore) including -
(dash character). You use ^
and $
to anchor beginning and end of checked text.
UrlRewriteFilter 文档说
执行规则时,过滤器将(非常简化)遍历所有
规则和每个做类似这样的伪代码:
When executing a rule the filter will (very simplified) loop over all rules and for each do something like this psuedo code:
Pattern.compile(<from> element); pattern.matcher(request url);
matcher.replaceAll(<to> element);
if ( <condition> elements match && matcher.find() ) {
handle <set> elements (if any)
execute <run> elements (if any)
perform <to> element (if any)
}
这就是你必须使用开头的原因( ^
)和结束( $
)字符串正则表达式锚点
That's why you have to use beginning (^
) and end ($
) string regular expression anchors
这篇关于Google App Engine上的Catch-all URL重写(Java)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!