Jetty Logo
Version: 9.2.21.v20170120
Contact the core Jetty developers at www.webtide.com

private support for your internal/customer projects ... custom extensions and distributions ... versioned snapshots for indefinite support ... scalability guidance for your apps and Ajax/Comet projects ... development services from 1 day to full product delivery

Rewrite Handler

Info
Usage
Rules

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.

Info

The standard Jetty distribution bundle contains the jetty-rewrite module, so all you need to do is to enable it using one of the module commands, eg:


$ java -jar start.jar --add-to-startd=rewrite
    

Note

If 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/test/rewrite/

Usage

The rewrite module enables the following jetty xml config file on the execution path:

As the commented out code shows, you configure the RewriteHandler by adding various rules.

There is an example of rules configuration in the standard distribution in the demo-base/etc/demo-rewrite-rules.xml file:

Embedded Example

This is an example for embedded Jetty, which does something similar to the configuration file example above:

Rules

There are several types of rules that are written extending useful base rule classes.

PatternRule

Matches against the request URI using the servlet pattern syntax.

CookiePatternRule

Adds a cookie to the response.

HeaderPatternRule

Adds/modifies a header in the response.

RedirectPatternRule

Redirects the response.

ResponsePatternRule

Sends the response code (status or error).

RewritePatternRule

Rewrite the URI by replacing the matched request path with a fixed string.

RegexRule

Matches against the request URI using regular expressions.

RedirectRegexRule

Redirect the response.

RewriteRegexRule

Rewrite the URI by matching with a regular expression. (The replacement string may use Template:$n to replace the nth capture group.)

HeaderRule

Match against request headers. Match either on a header name + specific value, or on the presence of a header (with any value).

ForwardedSchemaHeaderRule

Set the scheme on the request (defaulting to https).

Others

Oddball rules that defy classification.

MsieSslRule

Disables the keep alive for SSL from IE5 or IE6.

LegacyRule

Implements the legacy API of RewriteHandler

RuleContainer

Groups rules together. The contained rules will only be processed if the conditions for the RuleContainer evaluate to true.

VirtualHostRuleContainer

Groups rules that apply only to a specific virtual host or a set of virtual hosts

See an error or something missing? Contribute to this documentation at Github!(Generated: 2015-03-13T11:47:52-05:00)