Scenario:

  1. I'm using a jetty server.
  2. I have a webapp running on: myip/myWebApp

Question: How to redirect base url to base url/something

ie. http://190.23.30.1/ --> http://190.23.30.1/mywebapp

So when a user hits http://190.23.30.1/ my server will redirect to http://190.23.30.1/myWebApp

There may be a config based way to do this but a quick and dirty solution is to create an index page that redirects to the desired URL.

<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>MyWebApp</title>
        <meta http-equiv="refresh" content="0;URL='/mywebapp'" />
    </head>
    <body>
    </body>
</html>
  • Create a 'root' directory in jetty: mkdir /webapps/root
  • Create an index.html in /webapps/root/ with content like the above

Jetty has a Rewrite Handler that can be enabled using

$ java -jar start.jar --add-to-start=rewrite`

Following the examples given in the documentation, you can add a rule in the xml file:

<Configure id="Server" class="org.eclipse.jetty.server.Server">
  <Ref refid="Rewrite">
    <Call name="addRule">
      <Arg>
        <New class="org.eclipse.jetty.rewrite.handler.RedirectPatternRule">
          <Set name="pattern">/</Set>
          <Set name="location">/myWebApp/</Set>
        </New>
      </Arg>
    </Call>
  </Ref>
</Configure>

The equivalent for embedded Jetty configuration file would be:

Server server = new Server();
RewriteHandler rewrite = new RewriteHandler();

RedirectPatternRule redirect = new RedirectPatternRule();
redirect.setPattern("/");
redirect.setReplacement("/myWebApp/");
rewrite.addRule(redirect);

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.