如何配置Jetty URL重写处理程序?

时间:2016-08-10 11:36:26

标签: url-rewriting jetty jetty-9

我正在测试jetty 9重写功能,尝试重写以下网址:

http://localhost/rewritehttp://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,但http://localhost/rewrite给出了404错误 我配错了吗?

1 个答案:

答案 0 :(得分:0)

我必须执行:

java -jar start.jar --add-to-startd=rewrite

修改jetty-rewrite.xml而不是jetty.xml

<?xml version="1.0"?>
<!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>