Jetty/Howto/MovedContextHandler

How to redirect or move a context

The org.eclipse.jetty.server.handler.MovedContextHandler[1] may be used 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, like any other context, you can configure a list of virtual hosts, meaning that this context will only respond to requests with to one of the listed host names.

Suppose we had a context that was deployed at /foo, but that now we would like to deploy at the root context / instead. First we would reconfigure and redeploy our context on Jetty, but then we'd like a way for all the browsers who have bookmarked /foo to be told to go to the new path. We would 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, and also preserving pathinfo and query strings on the redirect: