title: Rewrite URLs in Tomcat from dynamic appName to existing context
body: <p>My Tomcat server is running two different contexts with URLs like this:</p>
  
  <p><strong>myhost.url/context1/…</strong></p>
  
  <p><strong>myhost.url/context2/…</strong></p>
  
  <p>I want to capture requests to:</p>
  
  <p><strong>myhost.url/appname/…</strong></p>
  
  <p>Where appname is dynamic, and redirect those requests to:</p>
  
  <p><strong>myhost.url/context2/appname/…</strong></p>
  
  <p>One constraint is to ignore any rewrite in case the request is addressed to <strong>myhost.url/context1/…</strong> or to some static files under the ROOT context.</p>
  
  <p>I want to use Tomcat's RewriteValve to rewrite the URLs hence I added an ROOT context with the following configuration:</p>
  
  <pre><code>&lt;?xml version='1.0' encoding='utf-8'?&gt;
  
  
  &lt;Context docBase="ROOT" reloadable="false" crossContext="true"&gt;
      &lt;Valve className="org.apache.catalina.valves.rewrite.RewriteValve"/&gt;
  &lt;/Context&gt;
  </code></pre>
  
  <p>And added a rewrite.config file under WEB-INF directory with the following configuration:</p>
  
  <pre><code>RewriteCond  %{CONTEXT_PATH} ^(?!/context1/).+$     [OR]
  RewriteCond  %{REQUEST_URI}  ^(?!/favicon.ico)          [OR]
  RewriteCond  %{REQUEST_URI}  ^(?!/blank.html)           [OR]
  RewriteCond  %{REQUEST_URI}  ^(?!/error.html)           [OR]
  RewriteCond  %{REQUEST_URI}  ^(?!/index.html)
  RewriteRule  ^/(.+)$                /context2/$1    [L]
  </code></pre>
  
  <p>Unfortunately this isn't work, please recommend on a correct rewrite rule.</p>