如何配置 Jetty URL 重写处理程序?
How to configure the Jetty URL rewrite handler?
我正在测试 jetty 9 重写功能,尝试重写以下 URL:
http://localhost/rewrite to http://localhost/test.html
这是我的配置:
<Configure id="Server" class="org.eclipse.jetty.server.Server">
<New id="Rewrite" class="org.eclipse.jetty.rewrite.handler.RewriteHandler">
<Set name="handler"><Ref refid="oldhandler"/></Set>
<Set name="rewriteRequestURI"><Property name="jetty.rewrite.rewriteRequestURI" deprecated="rewrite.rewriteRequestURI" default="true"/></Set>
<Set name="rewritePathInfo"><Property name="jetty.rewrite.rewritePathInfo" deprecated="rewrite.rewritePathInfo" default="false"/></Set>
<Set name="originalPathAttribute"><Property name="jetty.rewrite.originalPathAttribute" deprecated="rewrite.originalPathAttribute" default="requestedPath"/></Set>
<!-- Set DispatcherTypes -->
<Set name="dispatcherTypes">
<Array type="javax.servlet.DispatcherType">
<Item><Call class="javax.servlet.DispatcherType" name="valueOf"><Arg>REQUEST</Arg></Call></Item>
<Item><Call class="javax.servlet.DispatcherType" name="valueOf"><Arg>ASYNC</Arg></Call></Item>
</Array>
</Set>
<Call name="addRule">
<Arg>
<New class="org.eclipse.jetty.rewrite.handler.RewritePatternRule">
<Set name="pattern">/rewrite</Set>
<Set name="replacement">/test.html</Set>
</New>
</Arg>
</Call>
</New>
...
</Configure>
打开 http://127.0.0.1/test.html in the browser works but http://localhost/rewrite 出现 404 错误。
我是不是配置错了?
我必须执行:
java -jar start.jar --add-to-startd=rewrite
修改 jetty-rewrite.xml
而不是 jetty.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_3.dtd">
<Configure id="Server" class="org.eclipse.jetty.server.Server">
<!-- =========================================================== -->
<!-- Configure rewrite handler -->
<!-- =========================================================== -->
<Call name="insertHandler">
<Arg>
<New class="org.eclipse.jetty.rewrite.handler.RewriteHandler">
<Set name="rewriteRequestURI">
<Property name="jetty.rewrite.rewriteRequestURI" deprecated="rewrite.rewriteRequestURI" default="true" />
</Set>
<Set name="rewritePathInfo">
<Property name="jetty.rewrite.rewritePathInfo" deprecated="rewrite.rewritePathInfo" default="false" />
</Set>
<Set name="originalPathAttribute">
<Property name="jetty.rewrite.originalPathAttribute" deprecated="rewrite.originalPathAttribute" default="requestedPath" />
</Set>
<!-- Set DispatcherTypes -->
<Set name="dispatcherTypes">
<Array type="javax.servlet.DispatcherType">
<Item>
<Call class="javax.servlet.DispatcherType" name="valueOf">
<Arg>REQUEST</Arg>
</Call>
</Item>
<Item>
<Call class="javax.servlet.DispatcherType" name="valueOf">
<Arg>ASYNC</Arg>
</Call>
</Item>
</Array>
</Set>
<Get id="Rewrite" name="ruleContainer" />
<!-- see rewrite-compactpath.xml for example how to add a rule -->
<Call name="addRule">
<Arg>
<New class="org.eclipse.jetty.rewrite.handler.RewritePatternRule">
<Set name="pattern">/rewrite/test.html</Set>
<Set name="replacement">/test/test.html</Set>
</New>
</Arg>
</Call>
</New>
</Arg>
</Call>
</Configure>
我正在测试 jetty 9 重写功能,尝试重写以下 URL:
http://localhost/rewrite to http://localhost/test.html
这是我的配置:
<Configure id="Server" class="org.eclipse.jetty.server.Server">
<New id="Rewrite" class="org.eclipse.jetty.rewrite.handler.RewriteHandler">
<Set name="handler"><Ref refid="oldhandler"/></Set>
<Set name="rewriteRequestURI"><Property name="jetty.rewrite.rewriteRequestURI" deprecated="rewrite.rewriteRequestURI" default="true"/></Set>
<Set name="rewritePathInfo"><Property name="jetty.rewrite.rewritePathInfo" deprecated="rewrite.rewritePathInfo" default="false"/></Set>
<Set name="originalPathAttribute"><Property name="jetty.rewrite.originalPathAttribute" deprecated="rewrite.originalPathAttribute" default="requestedPath"/></Set>
<!-- Set DispatcherTypes -->
<Set name="dispatcherTypes">
<Array type="javax.servlet.DispatcherType">
<Item><Call class="javax.servlet.DispatcherType" name="valueOf"><Arg>REQUEST</Arg></Call></Item>
<Item><Call class="javax.servlet.DispatcherType" name="valueOf"><Arg>ASYNC</Arg></Call></Item>
</Array>
</Set>
<Call name="addRule">
<Arg>
<New class="org.eclipse.jetty.rewrite.handler.RewritePatternRule">
<Set name="pattern">/rewrite</Set>
<Set name="replacement">/test.html</Set>
</New>
</Arg>
</Call>
</New>
...
</Configure>
打开 http://127.0.0.1/test.html in the browser works but http://localhost/rewrite 出现 404 错误。
我是不是配置错了?
我必须执行:
java -jar start.jar --add-to-startd=rewrite
修改 jetty-rewrite.xml
而不是 jetty.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_3.dtd">
<Configure id="Server" class="org.eclipse.jetty.server.Server">
<!-- =========================================================== -->
<!-- Configure rewrite handler -->
<!-- =========================================================== -->
<Call name="insertHandler">
<Arg>
<New class="org.eclipse.jetty.rewrite.handler.RewriteHandler">
<Set name="rewriteRequestURI">
<Property name="jetty.rewrite.rewriteRequestURI" deprecated="rewrite.rewriteRequestURI" default="true" />
</Set>
<Set name="rewritePathInfo">
<Property name="jetty.rewrite.rewritePathInfo" deprecated="rewrite.rewritePathInfo" default="false" />
</Set>
<Set name="originalPathAttribute">
<Property name="jetty.rewrite.originalPathAttribute" deprecated="rewrite.originalPathAttribute" default="requestedPath" />
</Set>
<!-- Set DispatcherTypes -->
<Set name="dispatcherTypes">
<Array type="javax.servlet.DispatcherType">
<Item>
<Call class="javax.servlet.DispatcherType" name="valueOf">
<Arg>REQUEST</Arg>
</Call>
</Item>
<Item>
<Call class="javax.servlet.DispatcherType" name="valueOf">
<Arg>ASYNC</Arg>
</Call>
</Item>
</Array>
</Set>
<Get id="Rewrite" name="ruleContainer" />
<!-- see rewrite-compactpath.xml for example how to add a rule -->
<Call name="addRule">
<Arg>
<New class="org.eclipse.jetty.rewrite.handler.RewritePatternRule">
<Set name="pattern">/rewrite/test.html</Set>
<Set name="replacement">/test/test.html</Set>
</New>
</Arg>
</Call>
</New>
</Arg>
</Call>
</Configure>