4

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

2 Answers 2

1

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" type="String"></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);
0

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .