The RewriteHandler matches a request against a set of rules, and modifies the request accordingly for any rules that match. The most common use is to rewrite request URIs, but it is capable of much more: rules can also be configured to redirect the response, set a cookie or response code on the response, modify the header, etc.

Feature

Quick Start

The standard Jetty distribution bundle contains the jetty-rewrite module JAR, at lib/jetty-rewrite-*.jar, and a sample configuration file, at etc/jetty-rewrite.xml. To enable the rewrite module, using the sample configuration file, start up Jetty with this command:

Rewrite Module DemoIf you are running the standard Jetty distribution with the sample test webapp, there will be a demo of the rewrite module at http://localhost:8080/rewrite/

Configuring Rules

Configuration File Example

The rules are configured using jetty.xml syntax. This example file shows how to add the rewrite handler for the entire server:

<Configureid="Server"class="org.eclipse.jetty.server.Server"><!-- create and configure the rewrite handler --><Newid="Rewrite"class="org.eclipse.jetty.rewrite.handler.RewriteHandler"><Setname="rewriteRequestURI">true</Set><Setname="rewritePathInfo">false</Set><Setname="originalPathAttribute">requestedPath</Set><!-- redirect the response. This is a redirect which is visible to the browser. After the redirect, the browser address bar will show /redirected --><Callname="addRule"><Arg><Newclass="org.eclipse.jetty.rewrite.handler.RedirectPatternRule"><Setname="pattern">/redirect/*</Set><Setname="replacement">/redirected</Set></New></Arg></Call><!-- rewrite the request URI. This is an internal rewrite, visible to server, but the browser will still show /some/old/context --><Callname="addRule"><Arg><Newclass="org.eclipse.jetty.rewrite.handler.RewritePatternRule"><Setname="pattern">/some/old/context</Set><Setname="replacement">/some/new/context</Set></New></Arg></Call><!-- reverse the order of the path sections. Internal rewrite --><Callname="addRule"><Arg><Newclass="org.eclipse.jetty.rewrite.handler.RewriteRegexRule"><Setname="regex">/reverse/([^/]*)/(.*)</Set><Setname="replacement">/reverse/$2/$1</Set></New></Arg></Call></New><!-- add the rewrite handler to the server --><Setname="handler"><Refid="Rewrite"/></Set></Configure>