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);