سؤال

I am trying to add a rewrite rule to my run jetty run Eclipse plugin. I am using Jetty v 8.1.2 and supply a 'jetty-rewrite.xml' in the 'Additional Jetty.xml' Eclipse run configuration option. What I would like to achieve is rewriting the following URL

/hello/world?id=1

to

/

The rewrite works in so far as my local URL is updated correctly. However, regardless of what URL I enter (regardless if it matches the rewrite pattern or not), I get a 404 File not Found error from jetty. Note that '/' is mapped to 'index.html' in my web.xml. I can enter any URL (even the full path to index.html) and I get the same 404 error.

<?xml version="1.0" ?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure.dtd">
<Configure id="Server" class="org.eclipse.jetty.server.Server">
  <!-- create and configure the rewrite handler -->
  <New id="Rewrite" class="org.eclipse.jetty.rewrite.handler.RewriteHandler">
    <Set name="rewriteRequestURI">true</Set>
    <Set name="rewritePathInfo">false</Set>
    <Set name="originalPathAttribute">requestedPath</Set>

    <!-- redirect the response. This is a redirect which is visible to the browser.
       After the redirect, the browser address bar will show /redirected -->
    <Call name="addRule">
      <Arg>
        <New class="org.eclipse.jetty.rewrite.handler.RedirectPatternRule">
         <Set name="pattern">/hello/world/*</Set>
         <Set name="location">/</Set>
        </New>
      </Arg>
    </Call>
  </New>
  <!-- add the rewrite handler to the server -->
  <Set name="handler">
   <Ref id="Rewrite" />
  </Set>
</Configure>

I don't use any other jetty configuration files, except the default ones that are loaded by the run jetty run plugin. Thanks for any pointers.

هل كانت مفيدة؟

المحلول

Turns out the problem was that I hadn't realized that <Set name="handler"> essentially 'overwrites' my default handlers. So, to fix it, I changed the last few lines to

<!-- add the rewrite handler to the server -->
<Set name="handler">
<New class="org.eclipse.jetty.server.handler.HandlerCollection">
  <Set name="handlers">
    <Array type="org.eclipse.jetty.server.Handler">
      <Item>
        <Ref id="Rewrite" />
      </Item>
      <Item>
        <Ref id="oldhandler"/>
      </Item>
    </Array>
  </Set>
</New>
</Set>

where oldhandler refers to a previously declared <Get id="oldhandler" name="handler"/>

نصائح أخرى

I use jetty9, and I edit two files:

1) start.ini add one line: "--module=rewrite"

2) etc/jetty-rewrite.xml add: /yyy/(.*) /$1

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top