Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between revisions of "Jetty/Howto/MovedContextHandler"

< Jetty‎ | Howto
 
(6 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 
= How to redirect or move a context  =
 
= How to redirect or move a context  =
  
The [http://download.eclipse.org/jetty/stable-7/apidocs/org/eclipse/jetty/server/handler/MovedContextHandler.html org.eclipse.jetty.server.handler.MovedContextHandler]&nbsp; may be used to relocate or redirect a context that has changed context path and/or virtual hosts.
+
{{Jetty Redirect|http://www.eclipse.org/jetty/documentation/current/moved-context-handler.html}}
  
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.  
+
You can use the [http://download.eclipse.org/jetty/stable-7/apidocs/org/eclipse/jetty/server/handler/MovedContextHandler.html MovedContextHandler] to relocate or redirect a context that has changed context path and/or virtual hosts.  
  
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.  
+
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.  
  
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&nbsp; /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 /.  
+
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.  
  
Here's an example. We've chosen to do a permanent redirection, and also preserving pathinfo and query strings on the redirect:<br>  
+
Suppose you have a context deployed at /foo, but that now you want to deploy at the root context / instead.
 +
* First you reconfigure and redeploy the context on Jetty.
 +
* Next you need a way to redirect all the browsers who have bookmarked&nbsp; /foo to the new path. You 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. This is a permanent redirection, which also preserves pathinfo and query strings on the redirect:<br>  
 
<pre>&lt;?xml version="1.0"  encoding="ISO-8859-1"?&gt;
 
<pre>&lt;?xml version="1.0"  encoding="ISO-8859-1"?&gt;
&lt;!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "http://jetty.mortbay.org/configure.dtd"&gt;
+
&lt;!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure.dtd"&gt;
  
&lt;Configure class="org.mortbay.jetty.handler.MovedContextHandler"&gt;
+
&lt;Configure class="org.eclipse.jetty.server.handler.MovedContextHandler"&gt;
 
   &lt;Set name="contextPath"&gt;/foo&lt;/Set&gt;
 
   &lt;Set name="contextPath"&gt;/foo&lt;/Set&gt;
 
   &lt;Set name="newContextURL"&gt;/&lt;/Set&gt;
 
   &lt;Set name="newContextURL"&gt;/&lt;/Set&gt;

Latest revision as of 18:13, 24 April 2013

How to redirect or move a context

Warning2.png
Jetty 7 and Jetty 8 are now EOL (End of Life)




THIS IS NOT THE DOCUMENTATION YOU ARE LOOKING FOR!!!!!






All development and stable releases are being performed with Jetty 9 and Jetty 10.






This wiki is now officially out of date and all content has been moved to the Jetty Documentation Hub






Direct Link to updated documentation: http://www.eclipse.org/jetty/documentation/current/moved-context-handler.html


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 you have a context deployed at /foo, but that now you want to deploy at the root context / instead.

  • First you reconfigure and redeploy the context on Jetty.
  • Next you need a way to redirect all the browsers who have bookmarked  /foo to the new path. You 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. This is a permanent redirection, which also preserves 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>209.235.245.73</Item>
          <Item>127.0.0.73</Item>
          <Item>acme.org</Item>
          <Item>www.acme.org</Item>
          <Item>server.acme.org</Item>
    </Array>
  </Set>

</Configure>

Back to the top