Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9172147
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:16:37+00:00 2026-06-17T16:16:37+00:00

I am trying to implement a simple rewrite rule in an embedded Jetty server,

  • 0

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);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T16:16:38+00:00Added an answer on June 17, 2026 at 4:16 pm

    Replace the 2 lines …

    _ctx.setHandler(rewriter);
    _handlerCollection.setHandlers(new Handler[] {_ctx});
    

    with

    rewriter.setHandler(_ctx);
    _handlerCollection.setHandlers(new Handler[] {rewriter});
    

    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 ….

    server
    +--  HandlerCollection
         [0]-- WebAppContext
               +-- Your servlets and filters in web.xml
               +-- DefaultServlet
                   +-- RewriteHandler
    

    That means if the WebAppContext can’t handle the request, then the RewriteHandler is executed to see if it can handle the request. That will never happen, as WebAppContext is setup to use DefaultServlet if nothing else matches.

    The simple change suggested changes the tree to look like this …

    server
    +--  HandlerCollection
         [0]-- RewriteHandler
               +-- WebAppContext
                   +-- Your servlets and filters in web.xml
                   +-- DefaultServlet
    

    This will allow the RewriteHandler to do its thing before the WebAppContext is even asked.

    Note: you can also have your code utilize the HandlerCollection a bit more properly for this scenario as well.

    // _ctx.setHandler(rewriter);
    // rewriter.setHandler(_ctx);
    _handlerCollection.setHandlers(new Handler[] { rewriter, _ctx });
    

    This will result in the following tree

    server
    +--  HandlerCollection
         [0]-- RewriteHandler
         [1]-- WebAppContext
               +-- Your servlets and filters in web.xml
               +-- DefaultServlet
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

im trying to implement simple secured client server communiction using WCF. when im launching
I am trying to implement a simple UDP client and server. Server should receive
I'm trying to implement a simple web server on Linux that connects to the
I've been trying to implement simple low level keyhook using JNI and all went
I'm getting acquanted with Core Animation and trying to implement simple movement along a
Iam trying to implement a simple JMS(traditional not using springs) code in eclipse using
Just trying to implement a simple audit logging solution with Grails 2.0.2 and it
I am trying to implement a simple login servlet but it's not working properly.
I am trying to implement a simple observer pattern to update widgets with relevant
I am trying to implement a simple client that connects to a give address.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.