Dashboard > Jetty > ... > Jetty Documentation > RewriteHandler
RewriteHandler Log In | Sign Up   View a printable version of the current page.

Added by Dexter Ang , last edited by Zeph Harben on Apr 18, 2008  (view change) show comment
Labels: 
(None)

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

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.

  1. CookiePatternRule
    Adds a new cookie in response.
  2. HeaderPatternRule
    Add/modifies the HTTP headers in response.
  3. RedirectPatternRule
    Sets the redirect location.
  4. ResponsePatternRule
    Sets the status and error codes.
  5. RewritePatternRule
    Rewrites the requested URI.

B. RegexRule
This rule uses the regular expression syntax for pattern matching. Below are known subclasses of RegexRule.

  1. RewriteRegexRule
    Rewrites the requested URI using regular expression.

C. Others

  1. MSIESSLRule
    Disables the keep alive on SSL from IE5 or IE6.
  2. LegacyRule
    The old version of rewrite.

Sample Configuration

Below is a sample how to configure the rewrite handler in jetty-rewrite.xml.

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">
<!-- =========================================================== -->
    <!-- RewriteHandler Sample Configuration                         -->
    <!-- =========================================================== -->
    <!-- Set it to old handler the handler specified in jetty.xml -->
    <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>

      <!-- Add rule to protect against IE ssl bug -->
      <Call name="addRule">
        <Arg>
          <New class="org.mortbay.jetty.handler.rewrite.MsieSslRule"/>
        </Arg>
      </Call>

      <!-- protect favicon handling -->
      <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>

      <!-- use legacy API for some rewrites -->
      <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>

      <!-- add a regex rule -->
      <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
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
Site running on a free Atlassian Confluence Open Source Project License granted to The Codehaus. Evaluate Confluence today.
Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.6.2 Build:#919 Nov 26, 2007) - Bug/feature request - Contact Administrators