Am 29.01.2016 15:34, schrieb Joe Aldrich:
Hello,

I am using Tomcat 8.0.28 on Windows 10 and am having a problem with
the Rewrite Value. I must include the escaped form of an ampersand
'%26' in the output URL.

My rewrite.config has the following:

RewriteCond %{QUERY_STRING} ^(.*&)?SCID=8(&.*)?$
RewriteRule ^/(product|specs|avail-options|avail-category)\.php$
/Product.action?select=Model+4+\%26+4C [R=301,L,NE]

I am escaping the percent sign with a backslash, and I have tried
using the NE flag. However, Tomcat always is treating the percent
symbol as a back reference to the above RewriteCond. If I don't have a
second capture group, then I get a 500 error from a
NullPointerException.

The current tomcat code does not allow escaping of percent or dollar sign.

The parser just looks for percent (or dollar) and applies it either as a backreference (when it is followed by a digit), or a map.

I have not found any indication, that escaping is possible with httpd. Could you provide a link to the doc, that states it is possible?

If you are willing to build tomcat yourself, you could try the attached patch, which will allow escaping of percent signs by specifying them as %%.

Your example would thus look like "/Product.action?select=Model+4+%%26+4C".

Regards,
 Felix


I was working with the documentation on this page:

http://tomcat.apache.org/tomcat-8.0-doc/rewrite.html

The desired output URL would be:

http://www.domain.com/Product.html?select=Model+4+%26+4C

In the example given for the NE flag on the page reference above, the
percent sign is escaped by a backslash to prevent it from being
treated as a back-reference. This is not working for me. Instead I
get:

http://www.domain.com/Product.action?select=Model+4+\null6+4C

Where the "null" is due to an empty second back-reference.  I believe
this is a bug in that it is not escaping the percent sign (making it
impossible to create the %26 in the redirect URL). Or am I
misunderstanding something here?

As a side question, shouldn't an empty back-reference be blank instead
of adding 'null' to the URL?

Joseph B Aldrich
Junior Java Developer
P: 800.981.1540 | F: 715.254.0996
4848 Industrial Park Rd. Stevens Point. 54481


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org
diff --git a/java/org/apache/catalina/valves/rewrite/Substitution.java 
b/java/org/apache/catalina/valves/rewrite/Substitution.java
index 0f84792..fc23b92 100644
--- a/java/org/apache/catalina/valves/rewrite/Substitution.java
+++ b/java/org/apache/catalina/valves/rewrite/Substitution.java
@@ -186,7 +186,7 @@ public class Substitution {
                     newElement.n = Character.digit(sub.charAt(percentPos + 1), 
10);
                     pos = percentPos + 2;
                     elements.add(newElement);
-                } else {
+                } else if (sub.charAt(percentPos + 1) == '{'){
                     // %: server variable as %{variable}
                     SubstitutionElement newElement = null;
                     int open = sub.indexOf('{', percentPos);
@@ -218,6 +218,13 @@ public class Substitution {
                     }
                     pos = close + 1;
                     elements.add(newElement);
+                } else if (sub.charAt(percentPos + 1) == '%') {
+                    StaticElement percentSign = new StaticElement();
+                    percentSign.value = "%";
+                    elements.add(percentSign);
+                    pos = percentPos + 2;
+                } else {
+                    throw new IllegalArgumentException(sub + ": Missing digit, 
curly brace or percent sign.");
                 }
             }
         }
diff --git a/test/org/apache/catalina/valves/rewrite/TestRewriteValve.java 
b/test/org/apache/catalina/valves/rewrite/TestRewriteValve.java
index 47f9440..070519c 100644
--- a/test/org/apache/catalina/valves/rewrite/TestRewriteValve.java
+++ b/test/org/apache/catalina/valves/rewrite/TestRewriteValve.java
@@ -33,6 +33,11 @@ public class TestRewriteValve extends TomcatBaseTest {
     }
 
     @Test
+    public void testPercentSign() throws Exception {
+        doTestRewrite("RewriteRule ^(.*) /a/%%5A", "/", "/a/%255A");
+    }
+
+    @Test
     public void testNoopRewrite() throws Exception {
         doTestRewrite("RewriteRule ^(.*) $1", "/a/%255A", "/a/%255A");
     }
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org

Reply via email to