| <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 Stylesheets V1.79.1"><meta name="keywords" content="jetty, servlet, servlet-api, cometd, http, websocket, eclipse, maven, java, server, software"><link rel="home" href="index.html" title="Jetty"><link rel="up" href="advanced-extras.html" title="Chapter 18. Provided Servlets, Filters, and Handlers"><link rel="prev" href="error-handler.html" title="Error Handler"><link rel="next" href="runner.html" title="Chapter 19. 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"><link rel="stylesheet" href="css/highlighter/foundation.css"><script src="js/highlight.pack.js"></script><script> |
| hljs.initHighlightingOnLoad(); |
| </script><link type="text/css" rel="stylesheet" href="css/font-awesome/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.4.28.v20200408</span></td><td style="width: 50%"></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="fa fa-chevron-left" aria-hidden="true"></i> Previous</a> </td><th width="60%" align="center">Chapter 18. Provided Servlets, Filters, and Handlers<br><a accesskey="p" href="index.html"><i class="fa fa-home" aria-hidden="true"></i> Home</a></th><td width="20%" align="right"> <a accesskey="n" href="runner.html">Next <i class="fa fa-chevron-right" aria-hidden="true"></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 for sponsored feature development |
| </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 class="toc"><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#_usage_2">Usage</a></span></dt><dt><span class="section"><a href="rewrite-handler.html#rewrite-rules">Rules</a></span></dt></dl></div><p>The <code class="literal">RewriteHandler</code> 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><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem">Classname: org.eclipse.jetty.rewrite.handler.RewriteHandler</li><li class="listitem">Maven artifact: org.eclipse.jetty:jetty-rewrite</li><li class="listitem">Javadoc: <a class="link" href="http://www.eclipse.org/jetty/javadoc/9.4.28.v20200408/org/eclipse/jetty/rewrite/handler/RewriteHandler.html" target="_top">http://www.eclipse.org/jetty/javadoc/9.4.28.v20200408/org/eclipse/jetty/rewrite/handler/RewriteHandler.html</a></li></ul></div><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-start=rewrite</pre></div><div class="blockquote"><blockquote class="blockquote"><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="fa fa-asterisk" aria-hidden="true"></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 <a class="link" href="http://localhost:8080/test/rewrite/" target="_top">http://localhost:8080/test/rewrite/</a></p></div></blockquote></div></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="_usage_2"></a>Usage</h3></div></div></div><p>The rewrite module enables the following Jetty xml config file on the execution path:</p><pre 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"><code><?xml version="1.0"?><!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_3.dtd"> |
| |
| <Configure id="Server" class="org.eclipse.jetty.server.Server"> |
| |
| <!-- =========================================================== --> |
| <!-- configure rewrite handler --> |
| <!-- =========================================================== --> |
| <Call name="insertHandler"> |
| <Arg> |
| <New class="org.eclipse.jetty.rewrite.handler.RewriteHandler"> |
| <Set name="rewriteRequestURI"><Property name="jetty.rewrite.rewriteRequestURI" deprecated="rewrite.rewriteRequestURI" default="true"/></Set> |
| <Set name="rewritePathInfo"><Property name="jetty.rewrite.rewritePathInfo" deprecated="rewrite.rewritePathInfo" default="false"/></Set> |
| <Set name="originalPathAttribute"><Property name="jetty.rewrite.originalPathAttribute" deprecated="rewrite.originalPathAttribute" default="requestedPath"/></Set> |
| |
| <!-- Set DispatcherTypes --> |
| <Set name="dispatcherTypes"> |
| <Array type="javax.servlet.DispatcherType"> |
| <Item><Call class="javax.servlet.DispatcherType" name="valueOf"><Arg>REQUEST</Arg></Call></Item> |
| <Item><Call class="javax.servlet.DispatcherType" name="valueOf"><Arg>ASYNC</Arg></Call></Item> |
| </Array> |
| </Set> |
| |
| <Get id="Rewrite" name="ruleContainer"/> |
| |
| <!-- see rewrite-compactpath.xml for example how to add a rule --> |
| |
| </New> |
| </Arg> |
| </Call> |
| </Configure></code></pre><p>As the commented out code shows, you configure the <code class="literal">RewriteHandler</code> 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="literal">demo-base/etc/demo-rewrite-rules.xml</code> file:</p><pre 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"><code><?xml version="1.0"?><!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_3.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></code></pre><div class="section"><div class="titlepage"><div><div><h4 class="title"><a name="_embedded_example_2"></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><pre 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"><code> 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);</code></pre></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="_patternrule"></a>PatternRule</h4></div></div></div><p>Matches against the request URI using the servlet pattern syntax.</p><div class="variablelist"><dl class="variablelist"><dt><span class="term"><a class="link" href="http://www.eclipse.org/jetty/javadoc/9.4.28.v20200408/org/eclipse/jetty/rewrite/handler/CookiePatternRule.html" target="_top">CookiePatternRule</a></span></dt><dd>Adds a cookie to the response.</dd><dt><span class="term"><a class="link" href="http://www.eclipse.org/jetty/javadoc/9.4.28.v20200408/org/eclipse/jetty/rewrite/handler/HeaderPatternRule.html" target="_top">HeaderPatternRule</a></span></dt><dd>Adds/modifies a header in the response.</dd><dt><span class="term"><a class="link" href="http://www.eclipse.org/jetty/javadoc/9.4.28.v20200408/org/eclipse/jetty/rewrite/handler/RedirectPatternRule.html" target="_top">RedirectPatternRule</a></span></dt><dd>Redirects the response.</dd><dt><span class="term"><a class="link" href="http://www.eclipse.org/jetty/javadoc/9.4.28.v20200408/org/eclipse/jetty/rewrite/handler/ResponsePatternRule.html" target="_top">ResponsePatternRule</a></span></dt><dd>Sends the response code (status or error).</dd><dt><span class="term"><a class="link" href="http://www.eclipse.org/jetty/javadoc/9.4.28.v20200408/org/eclipse/jetty/rewrite/handler/RewritePatternRule.html" target="_top">RewritePatternRule</a></span></dt><dd>Rewrite the URI by replacing the matched request path with a fixed string.</dd></dl></div></div><div class="section"><div class="titlepage"><div><div><h4 class="title"><a name="_regexrule"></a>RegexRule</h4></div></div></div><p>Matches against the request URI using regular expressions.</p><div class="variablelist"><dl class="variablelist"><dt><span class="term"><a class="link" href="http://www.eclipse.org/jetty/javadoc/9.4.28.v20200408/org/eclipse/jetty/rewrite/handler/RedirectRegexRule.html" target="_top">RedirectRegexRule</a></span></dt><dd>Redirect the response.</dd><dt><span class="term"><a class="link" href="http://www.eclipse.org/jetty/javadoc/9.4.28.v20200408/org/eclipse/jetty/rewrite/handler/RewriteRegexRule.html" target="_top">RewriteRegexRule</a></span></dt><dd>Rewrite the URI by matching with a regular expression. |
| (The replacement string may use <code class="literal">Template:$n</code> to replace the nth capture group.)</dd></dl></div></div><div class="section"><div class="titlepage"><div><div><h4 class="title"><a name="_headerrule"></a>HeaderRule</h4></div></div></div><p>Match against request headers. Match either on a header name and specific value, or on the presence of a header (with any value).</p><div class="variablelist"><dl class="variablelist"><dt><span class="term"><a class="link" href="http://www.eclipse.org/jetty/javadoc/9.4.28.v20200408/org/eclipse/jetty/rewrite/handler/ForwardedSchemeHeaderRule.html" target="_top">ForwardedSchemaHeaderRule</a></span></dt><dd>Set the scheme on the request (defaulting to HTTPS).</dd></dl></div></div><div class="section"><div class="titlepage"><div><div><h4 class="title"><a name="_others"></a>Others</h4></div></div></div><p>Extra rules that defy standard classification.</p><div class="variablelist"><dl class="variablelist"><dt><span class="term"><a class="link" href="http://www.eclipse.org/jetty/javadoc/9.4.28.v20200408/org/eclipse/jetty/rewrite/handler/MsieSslRule.html" target="_top">MsieSslRule</a></span></dt><dd>Disables the keep alive for SSL from IE5 or IE6.</dd><dt><span class="term"><a class="link" href="http://www.eclipse.org/jetty/javadoc/9.4.28.v20200408/org/eclipse/jetty/rewrite/handler/LegacyRule.html" target="_top">LegacyRule</a></span></dt><dd>Implements the legacy API of RewriteHandler</dd></dl></div></div><div class="section"><div class="titlepage"><div><div><h4 class="title"><a name="_rulecontainer"></a>RuleContainer</h4></div></div></div><p>Groups rules together. |
| The contained rules will only be processed if the conditions for the <code class="literal">RuleContainer</code> evaluate to true.</p><div class="variablelist"><dl class="variablelist"><dt><span class="term"><a class="link" href="http://www.eclipse.org/jetty/javadoc/9.4.28.v20200408/org/eclipse/jetty/rewrite/handler/VirtualHostRuleContainer.html" target="_top">VirtualHostRuleContainer</a></span></dt><dd>Groups rules that apply only to a specific virtual host or a set of virtual hosts</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="fa fa-chevron-left" aria-hidden="true"></i> Previous</a> </td><td width="20%" align="center"><a accesskey="u" href="advanced-extras.html"><i class="fa fa-chevron-up" aria-hidden="true"></i> Top</a></td><td width="40%" align="right"> <a accesskey="n" href="runner.html">Next <i class="fa fa-chevron-right" aria-hidden="true"></i></a></td></tr><tr><td width="40%" align="left" valign="top">Error Handler </td><td width="20%" align="center"><a accesskey="h" href="index.html"><i class="fa fa-home" aria-hidden="true"></i> Home</a></td><td width="40%" align="right" valign="top"> Chapter 19. 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/eclipse/jetty.project">Contribute to this documentation at |
| <span class="website"><i class="fa fa-github" aria-hidden="true"></i> Github!</span></a></span><span style="float: right"><i>(Generated: 2020-04-08)</i></span></div></p></body></html> |