How to redirect or move a context
You can use the MovedContextHandler to relocate or redirect a context that has changed context path and/or virtual hosts.
You can configure it to permanently redirect the old url to the new url, in which case Jetty sends a Http Status code of 301 to the browser with the new url. Alternatively, you can make it non-permanent, in which case Jetty sends a 302 Http Status code along with the new url.
In addition, as with any other context, you can configure a list of virtual hosts, meaning that this context responds only to requests to one of the listed host names.
Suppose we have a context deployed at /foo, but that now we want to deploy at the root context / instead. First we reconfigure and redeploy our context on Jetty. Next we need a way to redirect all the browsers who have bookmarked /foo to the new path. We create a new context xml file in $JETTY_HOME/contexts and configure the MovedContextHandler to do the redirection from /foo to /.
Here's an example. We've chosen to do a permanent redirection, while also preserving pathinfo and query strings on the redirect:
<?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure.dtd"> <Configure class="org.eclipse.jetty.server.handler.MovedContextHandler"> <Set name="contextPath">/foo</Set> <Set name="newContextURL">/</Set> <Set name="permanent">true</Set> <Set name="discardPathInfo">false</Set> <Set name="discardQuery">false</Set> <Set name="virtualHosts"> <Array type="String"> <Item>18.104.22.168</Item> <Item>127.0.0.73</Item> <Item>acme.org</Item> <Item>www.acme.org</Item> <Item>server.acme.org</Item> </Array> </Set> </Configure>