I am trying to implement a simple rewrite rule in an embedded Jetty server, following the Jetty documentation examples.
I want requests to /admin/
to rewrite to /admin.html.
At the moment if I request /admin/
I get a 404 error with /admin.html
not found.
But if I request /admin.html
directly, it works!
There are 2 other similar posts on stackoverflow but no answers to the question!
Here's the code:
WebAppContext _ctx = new WebAppContext();
_ctx.setContextPath("/");
_ctx.setDefaultsDescriptor(JETTY_DEFAULTS_DESCRIPTOR);
_ctx.setParentLoaderPriority(true);
_ctx.setWar(getShadedWarUrl());
_ctx.setResourceBase(getShadedWarUrl());
RewriteHandler rewriter = new RewriteHandler();
rewriter.setRewritePathInfo(true);
rewriter.setRewriteRequestURI(true);
rewriter.setOriginalPathAttribute("requestedPath");
RewritePatternRule admin = new RewritePatternRule();
admin.setPattern("/admin/");
admin.setReplacement("/admin.html");
admin.setTerminating(true); // this will stop Jetty from chaining the rewrites
rewriter.addRule(admin);
_ctx.setHandler(rewriter);
HandlerCollection _handlerCollection = new HandlerCollection();
_handlerCollection.setHandlers(new Handler[] {_ctx});
server.setHandlers(_result);
Replace the 2 lines ...
with
That will make the rewriter rules kick in before the normal context handling.
Think of the context handling as a tree. In your example code you have ....
That means if the
WebAppContext
can't handle the request, then theRewriteHandler
is executed to see if it can handle the request. That will never happen, asWebAppContext
is setup to useDefaultServlet
if nothing else matches.The simple change suggested changes the tree to look like this ...
This will allow the
RewriteHandler
to do its thing before theWebAppContext
is even asked.Note: you can also have your code utilize the
HandlerCollection
a bit more properly for this scenario as well.This will result in the following tree