The RewriteHandler is inspired by the Apache's mod-rewrite. Aside from rewriting URI's it can do many things. See the rule list for more.
Rule list
The Rule is divided in 3 types: PatternRule, RegexRule and others. Each rules have a specific actions. And these actions are only invoked whenever the rule find its match. Below are the list of rules with their corresponding actions.
A. PatternRule
This rule uses servlet-mapping syntax for pattern matching. Below are known subclasses of PatternRule.
- CookiePatternRule
Adds a new cookie in response.
- HeaderPatternRule
Add/modifies the HTTP headers in response.
- RedirectPatternRule
Sets the redirect location.
- ResponsePatternRule
Sets the status and error codes.
- RewritePatternRule
Rewrites the requested URI.
B. RegexRule
This rule uses the regular expression syntax for pattern matching. Below are known subclasses of RegexRule.
- RewriteRegexRule
Rewrites the requested URI using regular expression.
C. Others
- MSIESSLRule
Disables the keep alive on SSL from IE5 or IE6.
- LegacyRule
The old version of rewrite.
Sample Configuration
Below is a sample how to configure the rewrite handler in jetty-rewrite.xml.
<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "http://jetty.mortbay.org/configure.dtd">
<Configure id="Server" class="org.mortbay.jetty.Server">
<Get id="oldhandler" name="handler"/>
<Set name="handler">
<New id="Rewrite" class="org.mortbay.jetty.handler.rewrite.RewriteHandler">
<Set name="handler"><Ref id="oldhandler"/></Set>
<Set name="rewriteRequestURI">true</Set>
<Set name="rewritePathInfo">false</Set>
<Set name="originalPathAttribute">requestedPath</Set>
<Call name="addRule">
<Arg>
<New class="org.mortbay.jetty.handler.rewrite.MsieSslRule"/>
</Arg>
</Call>
<Call name="addRule">
<Arg>
<New class="org.mortbay.jetty.handler.rewrite.HeaderPatternRule">
<Set name="pattern">/favicon.ico</Set>
<Set name="name">Cache-Control</Set>
<Set name="value">Max-Age=3600,public</Set>
<Set name="terminating">true</Set>
</New>
</Arg>
</Call>
<Call name="addRewriteRule">
<Arg>/some/old/context/*</Arg>
<Arg>/test/dump/newcontext</Arg>
</Call>
<Call name="addRewriteRule">
<Arg>/test/dump/rewrite/*</Arg>
<Arg>/test/dump/rewritten</Arg>
</Call>
<Call name="addRewriteRule">
<Arg>/test/dump/rewrite/protect/*</Arg>
<Arg/>
</Call>
<Call name="addRewriteRule">
<Arg>/test/*</Arg>
<Arg/>
</Call>
<Call name="addRewriteRule">
<Arg>/*</Arg>
<Arg>/test</Arg>
</Call>
<Call name="addRule">
<Arg>
<New class="org.mortbay.jetty.handler.rewrite.RewriteRegexRule">
<Set name="regex">/test/dump/regex/([^/]*)/(.*)</Set>
<Set name="replacement">/test/dump/$2/$1</Set>
</New>
</Arg>
</Call>
</New>
</Set>
</Configure>
To use this configuration, include the configuration file during jetty startup. Example below.
java -jar start.jar etc/jetty.xml etc/jetty-rewrite.xml