blob: 9a62516ffc1d64519071c025d8b37b47b2ce4eeb [file]
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Rewrite Handler</title><link rel="stylesheet" type="text/css" href="css/docbook.css"><meta name="generator" content="DocBook XSL-NS Stylesheets V1.76.1"><meta name="keywords" content="jetty, servlet, servlet-api, cometd, http, spdy, websocket, eclipse, maven, java, server, software"><link rel="home" href="index.html" title="Jetty : The Definitive Reference"><link rel="up" href="advanced-extras.html" title="Chapter&nbsp;18.&nbsp;Provided Servlets, Filters, and Handlers"><link rel="prev" href="error-handler.html" title="Error Handler"><link rel="next" href="runner.html" title="Chapter&nbsp;19.&nbsp;Jetty Runner"><link xmlns:jfetch="java:org.eclipse.jetty.xslt.tools.JavaSourceFetchExtension" xmlns:fetch="java:org.eclipse.jetty.xslt.tools.SourceFetchExtension" xmlns:d="http://docbook.org/ns/docbook" xmlns:l="http://docbook.sourceforge.net/xmlns/l10n/1.0" xmlns:xslthl="http://xslthl.sf.net" xmlns:gcse="http://www.google.com" xmlns:date="http://exslt.org/dates-and-times" rel="shortcut icon" href="images/favicon.ico"><script type="text/javascript" src="js/shCore.js"></script><script type="text/javascript" src="js/shBrushJava.js"></script><script type="text/javascript" src="js/shBrushXml.js"></script><script type="text/javascript" src="js/shBrushBash.js"></script><script type="text/javascript" src="js/shBrushJScript.js"></script><script type="text/javascript" src="js/shBrushSql.js"></script><script type="text/javascript" src="js/shBrushProperties.js"></script><script type="text/javascript" src="js/shBrushPlain.js"></script><link type="text/css" rel="stylesheet" href="css/shCore.css"><link type="text/css" rel="stylesheet" href="css/shThemeEclipse.css"><link type="text/css" rel="stylesheet" href="css/font-awesome.min.css"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><table xmlns:jfetch="java:org.eclipse.jetty.xslt.tools.JavaSourceFetchExtension" xmlns:fetch="java:org.eclipse.jetty.xslt.tools.SourceFetchExtension" xmlns:d="http://docbook.org/ns/docbook" xmlns:l="http://docbook.sourceforge.net/xmlns/l10n/1.0" xmlns:xslthl="http://xslthl.sf.net" xmlns:gcse="http://www.google.com" xmlns:date="http://exslt.org/dates-and-times"><tr><td style="width: 25%"><a href="http://www.eclipse.org/jetty"><img src="images/jetty-header-logo.png" alt="Jetty Logo"></a><br><span style="font-size: small">
Version: 9.2.21.v20170120</span></td><td style="width: 50%"><script type="text/javascript"> (function() {
var cx = '016459005284625897022:obd4lsai2ds';
var gcse = document.createElement('script');
gcse.type = 'text/javascript';
gcse.async = true;
gcse.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') +
'//www.google.com/cse/cse.js?cx=' + cx;
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(gcse, s);
})();
</script><gcse:search></gcse:search></td></tr></table><div xmlns:jfetch="java:org.eclipse.jetty.xslt.tools.JavaSourceFetchExtension" xmlns:fetch="java:org.eclipse.jetty.xslt.tools.SourceFetchExtension" xmlns:d="http://docbook.org/ns/docbook" xmlns:l="http://docbook.sourceforge.net/xmlns/l10n/1.0" xmlns:xslthl="http://xslthl.sf.net" xmlns:gcse="http://www.google.com" xmlns:date="http://exslt.org/dates-and-times" class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Rewrite Handler</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="error-handler.html"><i class="icon-chevron-left"></i> Previous</a>&nbsp;</td><th width="60%" align="center">Chapter&nbsp;18.&nbsp;Provided Servlets, Filters, and Handlers<br><a accesskey="p" href="index.html"><i class="icon-home"></i> Home</a></th><td width="20%" align="right">&nbsp;<a accesskey="n" href="runner.html">Next <i class="icon-chevron-right"></i></a></td></tr></table><hr></div><div xmlns:jfetch="java:org.eclipse.jetty.xslt.tools.JavaSourceFetchExtension" xmlns:fetch="java:org.eclipse.jetty.xslt.tools.SourceFetchExtension" xmlns:d="http://docbook.org/ns/docbook" xmlns:l="http://docbook.sourceforge.net/xmlns/l10n/1.0" xmlns:xslthl="http://xslthl.sf.net" xmlns:gcse="http://www.google.com" xmlns:date="http://exslt.org/dates-and-times" class="jetty-callout"><h5 class="callout"><a href="http://www.webtide.com/">Contact the core Jetty developers at
<span class="website">www.webtide.com</span></a></h5><p>
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
</p></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="rewrite-handler"></a>Rewrite Handler</h2></div></div></div><div class="toc"><dl><dt><span class="section"><a href="rewrite-handler.html#rewrite-handler-metadata">Info</a></span></dt><dt><span class="section"><a href="rewrite-handler.html#d0e14114">Usage</a></span></dt><dt><span class="section"><a href="rewrite-handler.html#rewrite-rules">Rules</a></span></dt></dl></div><p>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.</p><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="rewrite-handler-metadata"></a>Info</h3></div></div></div><p>
</p><div class="itemizedlist"><ul class="itemizedlist" type="disc"><li class="listitem"><p>Classname:
org.eclipse.jetty.rewrite.handler.RewriteHandler</p></li><li class="listitem"><p>Maven artifact: org.eclipse.jetty:jetty-rewrite</p></li><li class="listitem"><p>Javadoc: <a class="link" href="???" target="_top">http://download.eclipse.org/jetty/stable-9/apidocs/org/eclipse/jetty/rewrite/handler/RewriteHandler.html</a></p></li><li class="listitem"><p>Xref: <a class="link" href="http://download.eclipse.org/jetty/stable-9/xref/org/eclipse/jetty/rewrite/handler/RewriteHandler.html" target="_top">http://download.eclipse.org/jetty/stable-9/xref/org/eclipse/jetty/rewrite/handler/RewriteHandler.html</a></p></li></ul></div><p>
</p><p>The standard Jetty distribution bundle contains the
<code class="literal">jetty-rewrite</code> <a class="link" href="startup-modules.html" title="Managing Startup Modules">module</a>, so all you need to do is to
enable it using one of the <a class="link" href="start-jar.html" title="Using start.jar">module
commands</a>, eg:</p><div class="screenexample"><pre class="screen">
$ java -jar start.jar --add-to-startd=rewrite
</pre></div></div><div xmlns:jfetch="java:org.eclipse.jetty.xslt.tools.JavaSourceFetchExtension" xmlns:fetch="java:org.eclipse.jetty.xslt.tools.SourceFetchExtension" xmlns:d="http://docbook.org/ns/docbook" xmlns:l="http://docbook.sourceforge.net/xmlns/l10n/1.0" xmlns:xslthl="http://xslthl.sf.net" xmlns:gcse="http://www.google.com" xmlns:date="http://exslt.org/dates-and-times" class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><h3 class="title"><i class="icon-asterisk"></i> Note</h3><p>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/</p></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="d0e14114"></a>Usage</h3></div></div></div><p>The rewrite module enables the following jetty xml config file on
the execution path:</p><div class="informalexample"><script type="syntaxhighlighter" class="brush: xml;toolbar: false">
<![CDATA[<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_0.dtd">
<!-- =============================================================== -->
<!-- Mixin the RewriteHandler -->
<!-- =============================================================== -->
<Configure id="Server" class="org.eclipse.jetty.server.Server">
<!-- =========================================================== -->
<!-- configure rewrite handler -->
<!-- =========================================================== -->
<Get id="oldhandler" name="handler"/>
<Set name="handler">
<New id="Rewrite" class="org.eclipse.jetty.rewrite.handler.RewriteHandler">
<Set name="handler"><Ref refid="oldhandler"/></Set>
<Set name="rewriteRequestURI"><Property name="rewrite.rewriteRequestURI" default="true"/></Set>
<Set name="rewritePathInfo"><Property name="rewrite.rewritePathInfo" default="false"/></Set>
<Set name="originalPathAttribute"><Property name="rewrite.originalPathAttribute" default="requestedPath"/></Set>
</New>
</Set>
<!-- example rule -->
<!--
<Call name="addRule">
<Arg>
<New class="org.eclipse.jetty.rewrite.handler.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>
-->
<!-- for example rules see jetty-demo.xml -->
</Configure>
]]>
</script></div><p>As the commented out code shows, you configure the RewriteHandler by
adding various rules. </p><p>There is an example of <a class="link" href="rewrite-handler.html#rewrite-rules" title="Rules">rules</a>
configuration in the standard distribution in the
<code class="filename">demo-base/etc/demo-rewrite-rules.xml</code> file:</p><div class="informalexample"><script type="syntaxhighlighter" class="brush: xml;toolbar: false">
<![CDATA[<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_0.dtd">
<!-- =============================================================== -->
<!-- Configure the demos -->
<!-- =============================================================== -->
<Configure id="Server" class="org.eclipse.jetty.server.Server">
<!-- ============================================================= -->
<!-- Add rewrite rules -->
<!-- ============================================================= -->
<Ref refid="Rewrite">
<!-- Add rule to protect against IE ssl bug -->
<Call name="addRule">
<Arg>
<New class="org.eclipse.jetty.rewrite.handler.MsieSslRule"/>
</Arg>
</Call>
<!-- protect favicon handling -->
<Call name="addRule">
<Arg>
<New class="org.eclipse.jetty.rewrite.handler.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>
<!-- redirect from the welcome page to a specific page -->
<Call name="addRule">
<Arg>
<New class="org.eclipse.jetty.rewrite.handler.RewritePatternRule">
<Set name="pattern">/test/rewrite/</Set>
<Set name="replacement">/test/rewrite/info.html</Set>
</New>
</Arg>
</Call>
<!-- replace the entire request URI -->
<Call name="addRule">
<Arg>
<New class="org.eclipse.jetty.rewrite.handler.RewritePatternRule">
<Set name="pattern">/test/some/old/context</Set>
<Set name="replacement">/test/rewritten/newcontext</Set>
</New>
</Arg>
</Call>
<!-- replace the beginning of the request URI -->
<Call name="addRule">
<Arg>
<New class="org.eclipse.jetty.rewrite.handler.RewritePatternRule">
<Set name="pattern">/test/rewrite/for/*</Set>
<Set name="replacement">/test/rewritten/</Set>
</New>
</Arg>
</Call>
<!-- reverse the order of the path sections -->
<Call name="addRule">
<Arg>
<New class="org.eclipse.jetty.rewrite.handler.RewriteRegexRule">
<Set name="regex">(.*?)/reverse/([^/]*)/(.*)</Set>
<Set name="replacement">$1/reverse/$3/$2</Set>
</New>
</Arg>
</Call>
<!-- add a cookie to each path visited -->
<Call name="addRule">
<Arg>
<New class="org.eclipse.jetty.rewrite.handler.CookiePatternRule">
<Set name="pattern">/*</Set>
<Set name="name">visited</Set>
<Set name="value">yes</Set>
</New>
</Arg>
</Call>
<!-- actual redirect, instead of internal rewrite -->
<Call name="addRule">
<Arg>
<New class="org.eclipse.jetty.rewrite.handler.RedirectPatternRule">
<Set name="pattern">/test/redirect/*</Set>
<Set name="location">/test/redirected</Set>
</New>
</Arg>
</Call>
<!-- add a response rule -->
<Call name="addRule">
<Arg>
<New class="org.eclipse.jetty.rewrite.handler.ResponsePatternRule">
<Set name="pattern">/400Error</Set>
<Set name="code">400</Set>
<Set name="reason">ResponsePatternRule Demo</Set>
</New>
</Arg>
</Call>
</Ref>
</Configure>
]]>
</script></div><div class="section"><div class="titlepage"><div><div><h4 class="title"><a name="d0e14138"></a>Embedded Example</h4></div></div></div><p>This is an example for embedded Jetty, which does something
similar to the configuration file example above:</p><div class="informalexample"><script type="syntaxhighlighter" class="brush: java;toolbar: false">
<![CDATA[
Server server = new Server();
RewriteHandler rewrite = new RewriteHandler();
rewrite.setRewriteRequestURI(true);
rewrite.setRewritePathInfo(false);
rewrite.originalPathAttribute("requestedPath");
RedirectPatternRule redirect = new RedirectPatternRule();
redirect.setPattern("/redirect/*");
redirect.setReplacement("/redirected");
rewrite.addRule(redirect);
RewritePatternRule oldToNew = new RewritePatternRule();
oldToNew.setPattern("/some/old/context");
oldToNew.setReplacement("/some/new/context");
rewrite.addRule(oldToNew);
RewriteRegexRule reverse = new RewriteRegexRule();
reverse.setRegex("/reverse/([^/]*)/(.*)");
reverse.setReplacement("/reverse/$2/$1");
rewrite.addRule(reverse);
server.setHandler(rewrite);
]]>
</script></div></div></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="rewrite-rules"></a>Rules</h3></div></div></div><p>There are several types of rules that are written extending useful
base rule classes.</p><div class="section"><div class="titlepage"><div><div><h4 class="title"><a name="d0e14151"></a>PatternRule</h4></div></div></div><p>Matches against the request URI using the servlet pattern
syntax.</p><div class="variablelist"><dl><dt><span class="term">
<a class="link" href="http://download.eclipse.org/jetty/stable-9/xref/org/eclipse/jetty/rewrite/handler/CookiePatternRule.html" target="_top">CookiePatternRule</a>
</span></dt><dd><p>Adds a cookie to the response.</p></dd><dt><span class="term">
<a class="link" href="http://download.eclipse.org/jetty/stable-9/xref/org/eclipse/jetty/rewrite/handler/HeaderPatternRule.html" target="_top">HeaderPatternRule</a>
</span></dt><dd><p>Adds/modifies a header in the response.</p></dd><dt><span class="term">
<a class="link" href="http://download.eclipse.org/jetty/stable-9/xref/org/eclipse/jetty/rewrite/handler/RedirectPatternRule.html" target="_top">RedirectPatternRule</a>
</span></dt><dd><p>Redirects the response.</p></dd><dt><span class="term">
<a class="link" href="http://download.eclipse.org/jetty/stable-9/xref/org/eclipse/jetty/rewrite/handler/ResponsePatternRule.html" target="_top">ResponsePatternRule</a>
</span></dt><dd><p>Sends the response code (status or error).</p></dd><dt><span class="term">
<a class="link" href="http://download.eclipse.org/jetty/stable-9/xref/org/eclipse/jetty/rewrite/handler/RewritePatternRule.html" target="_top">RewritePatternRule</a>
</span></dt><dd><p>Rewrite the URI by replacing the matched request path with a
fixed string.</p></dd></dl></div></div><div class="section"><div class="titlepage"><div><div><h4 class="title"><a name="d0e14202"></a>RegexRule</h4></div></div></div><p>Matches against the request URI using regular expressions.</p><div class="variablelist"><dl><dt><span class="term">
<a class="link" href="http://download.eclipse.org/jetty/stable-9/xref/org/eclipse/jetty/rewrite/handler/RedirectRegexRule.html" target="_top">RedirectRegexRule</a>
</span></dt><dd><p>Redirect the response.</p></dd><dt><span class="term">
<a class="link" href="http://download.eclipse.org/jetty/stable-9/xref/org/eclipse/jetty/rewrite/handler/RewriteRegexRule.html" target="_top">RewriteRegexRule</a>
</span></dt><dd><p>Rewrite the URI by matching with a regular expression. (The
replacement string may use Template:$n to replace the nth capture
group.)</p></dd></dl></div></div><div class="section"><div class="titlepage"><div><div><h4 class="title"><a name="d0e14226"></a>HeaderRule</h4></div></div></div><p>Match against request headers. Match either on a header name +
specific value, or on the presence of a header (with any value).</p><div class="variablelist"><dl><dt><span class="term">
<a class="link" href="http://download.eclipse.org/jetty/stable-9/xref/org/eclipse/jetty/rewrite/handler/ForwardedSchemeHeaderRule.html" target="_top">ForwardedSchemaHeaderRule</a>
</span></dt><dd><p>Set the scheme on the request (defaulting to https).</p></dd></dl></div></div><div class="section"><div class="titlepage"><div><div><h4 class="title"><a name="d0e14241"></a>Others</h4></div></div></div><p>Oddball rules that defy classification.</p><div class="variablelist"><dl><dt><span class="term">
<a class="link" href="http://download.eclipse.org/jetty/stable-9/xref/org/eclipse/jetty/rewrite/handler/MsieSslRule.html" target="_top">MsieSslRule</a>
</span></dt><dd><p>Disables the keep alive for SSL from IE5 or IE6.</p></dd><dt><span class="term">
<a class="link" href="http://download.eclipse.org/jetty/stable-9/xref/org/eclipse/jetty/rewrite/handler/LegacyRule.html" target="_top">LegacyRule</a>
</span></dt><dd><p>Implements the legacy API of RewriteHandler</p></dd></dl></div></div><div class="section"><div class="titlepage"><div><div><h4 class="title"><a name="d0e14265"></a>RuleContainer</h4></div></div></div><p>Groups rules together. The contained rules will only be processed
if the conditions for the RuleContainer evaluate to true.</p><div class="variablelist"><dl><dt><span class="term">
<a class="link" href="http://download.eclipse.org/jetty/stable-9/xref/org/eclipse/jetty/rewrite/handler/VirtualHostRuleContainer.html" target="_top">VirtualHostRuleContainer</a>
</span></dt><dd><p>Groups rules that apply only to a specific virtual host or a
set of virtual hosts</p></dd></dl></div></div></div></div><script type="text/javascript">
SyntaxHighlighter.all()
</script><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="error-handler.html"><i class="icon-chevron-left"></i> Previous</a>&nbsp;</td><td width="20%" align="center"><a accesskey="u" href="advanced-extras.html"><i class="icon-chevron-up"></i> Top</a></td><td width="40%" align="right">&nbsp;<a accesskey="n" href="runner.html">Next <i class="icon-chevron-right"></i></a></td></tr><tr><td width="40%" align="left" valign="top">Error Handler&nbsp;</td><td width="20%" align="center"><a accesskey="h" href="index.html"><i class="icon-home"></i> Home</a></td><td width="40%" align="right" valign="top">&nbsp;Chapter&nbsp;19.&nbsp;Jetty Runner</td></tr></table></div><p xmlns:jfetch="java:org.eclipse.jetty.xslt.tools.JavaSourceFetchExtension" xmlns:fetch="java:org.eclipse.jetty.xslt.tools.SourceFetchExtension" xmlns:d="http://docbook.org/ns/docbook" xmlns:l="http://docbook.sourceforge.net/xmlns/l10n/1.0" xmlns:xslthl="http://xslthl.sf.net" xmlns:gcse="http://www.google.com" xmlns:date="http://exslt.org/dates-and-times"><div class="jetty-callout">
See an error or something missing?
<span class="callout"><a href="http://github.com/jetty-project/jetty-documentation">Contribute to this documentation at
<span class="website"><i class="icon-github"></i> Github!</span></a></span><span style="float: right"><i>(Generated: 2015-03-13T11:47:52-05:00)</i></span></div></p><script xmlns:jfetch="java:org.eclipse.jetty.xslt.tools.JavaSourceFetchExtension" xmlns:fetch="java:org.eclipse.jetty.xslt.tools.SourceFetchExtension" xmlns:d="http://docbook.org/ns/docbook" xmlns:l="http://docbook.sourceforge.net/xmlns/l10n/1.0" xmlns:xslthl="http://xslthl.sf.net" xmlns:gcse="http://www.google.com" xmlns:date="http://exslt.org/dates-and-times" type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-1149868-7']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script></body></html>