]> granicus.if.org Git - apache/blobdiff - modules/mappers/mod_rewrite.c
Move the POSIX reg* implementations into the ap_* namespace;
[apache] / modules / mappers / mod_rewrite.c
index 98613d8fc4bc172fd466923cfb139aa0d612ccc3..aab63cbe597d92b8d2e59266b2367662609a3651 100644 (file)
@@ -1,59 +1,17 @@
-/* ====================================================================
- * The Apache Software License, Version 1.1
+/* Copyright 1999-2005 The Apache Software Foundation or its licensors, as
+ * applicable.
  *
- * Copyright (c) 2000-2003 The Apache Software Foundation.  All rights
- * reserved.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
  *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
+ *     http://www.apache.org/licenses/LICENSE-2.0
  *
- * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- *
- * 2. Redistributions in binary form must reproduce the above copyright
- *    notice, this list of conditions and the following disclaimer in
- *    the documentation and/or other materials provided with the
- *    distribution.
- *
- * 3. The end-user documentation included with the redistribution,
- *    if any, must include the following acknowledgment:
- *       "This product includes software developed by the
- *        Apache Software Foundation (http://www.apache.org/)."
- *    Alternately, this acknowledgment may appear in the software itself,
- *    if and wherever such third-party acknowledgments normally appear.
- *
- * 4. The names "Apache" and "Apache Software Foundation" must
- *    not be used to endorse or promote products derived from this
- *    software without prior written permission. For written
- *    permission, please contact apache@apache.org.
- *
- * 5. Products derived from this software may not be called "Apache",
- *    nor may "Apache" appear in their name, without prior written
- *    permission of the Apache Software Foundation.
- *
- * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
- * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
- * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
- * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
- * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
- * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
- * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- * ====================================================================
- *
- * This software consists of voluntary contributions made by many
- * individuals on behalf of the Apache Software Foundation.  For more
- * information on the Apache Software Foundation, please see
- * <http://www.apache.org/>.
- *
- * Portions of this software are based upon public domain software
- * originally written at the National Center for Supercomputing Applications,
- * University of Illinois, Urbana-Champaign.
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
  */
 
 /*                       _                            _ _
@@ -84,7 +42,7 @@
  *  gifted exclusively to the The Apache Software Foundation in July 1997 by
  *
  *      Ralf S. Engelschall
- *      rse@engelschall.com
+ *      rse engelschall.com
  *      www.engelschall.com
  */
 
 #include "http_protocol.h"
 #include "http_vhost.h"
 
+#include "mod_ssl.h"
+
 #include "mod_rewrite.h"
 
-#if !defined(OS2) && !defined(WIN32) && !defined(BEOS)  && !defined(NETWARE)
+#ifdef AP_NEED_SET_MUTEX_PERMS
 #include "unixd.h"
-#define MOD_REWRITE_SET_MUTEX_PERMS /* XXX Apache should define something */
 #endif
 
 /*
- * The key in the r->notes apr_table_t wherein we store our accumulated
- * Vary values, and the one used for per-condition checks in a chain.
+ * in order to improve performance on running production systems, you
+ * may strip all rewritelog code entirely from mod_rewrite by using the
+ * -DREWRITELOG_DISABLED compiler option.
+ *
+ * DO NOT USE THIS OPTION FOR PUBLIC BINARY RELEASES. Otherwise YOU are
+ * responsible for answering all the mod_rewrite questions out there.
  */
-#define VARY_KEY "rewrite-Vary"
-#define VARY_KEY_THIS "rewrite-Vary-this"
+#ifndef REWRITELOG_DISABLED
+
+#define rewritelog(x) do_rewritelog x
+#define REWRITELOG_MODE  ( APR_UREAD | APR_UWRITE | APR_GREAD | APR_WREAD )
+#define REWRITELOG_FLAGS ( APR_WRITE | APR_APPEND | APR_CREATE )
+
+#else /* !REWRITELOG_DISABLED */
+
+#define rewritelog(x)
+
+#endif /* REWRITELOG_DISABLED */
 
 /* remembered mime-type for [T=...] */
 #define REWRITE_FORCED_MIMETYPE_NOTEVAR "rewrite-forced-mimetype"
+#define REWRITE_FORCED_HANDLER_NOTEVAR  "rewrite-forced-handler"
 
 #define ENVVAR_SCRIPT_URL "SCRIPT_URL"
 #define REDIRECT_ENVVAR_SCRIPT_URL "REDIRECT_" ENVVAR_SCRIPT_URL
 #define RULEFLAG_NOTMATCH           1<<6
 #define RULEFLAG_PROXY              1<<7
 #define RULEFLAG_PASSTHROUGH        1<<8
-#define RULEFLAG_FORBIDDEN          1<<9
-#define RULEFLAG_GONE               1<<10
-#define RULEFLAG_QSAPPEND           1<<11
-#define RULEFLAG_NOCASE             1<<12
-#define RULEFLAG_NOESCAPE           1<<13
+#define RULEFLAG_QSAPPEND           1<<9
+#define RULEFLAG_NOCASE             1<<10
+#define RULEFLAG_NOESCAPE           1<<11
+#define RULEFLAG_NOSUB              1<<12
+#define RULEFLAG_STATUS             1<<13
 
 /* return code of the rewrite rule
  * the result may be escaped - or not
  */
 #define ACTION_NORMAL               1<<0
 #define ACTION_NOESCAPE             1<<1
+#define ACTION_STATUS               1<<2
 
 
 #define MAPTYPE_TXT                 1<<0
 /* XXX: not used at all. We should do a check somewhere and/or cut the cookie */
 #define MAX_COOKIE_LEN 4096
 
-/* max number of regex captures */
-#define MAX_NMATCH 10
-
-/* default maximum number of internal redirects */
-#define REWRITE_REDIRECT_LIMIT 10
-
-/* for rewrite log file */
-#define REWRITELOG_MODE  ( APR_UREAD | APR_UWRITE | APR_GREAD | APR_WREAD )
-#define REWRITELOG_FLAGS ( APR_WRITE | APR_APPEND | APR_CREATE )
-
 /* max line length (incl.\n) in text rewrite maps */
 #ifndef REWRITE_MAX_TXT_MAP_LINE
 #define REWRITE_MAX_TXT_MAP_LINE 1024
 #endif
 
-/* max response length (incl.\n) in prg rewrite maps */
-#ifndef REWRITE_MAX_PRG_MAP_LINE
-#define REWRITE_MAX_PRG_MAP_LINE 2048
+/* buffer length for prg rewrite maps */
+#ifndef REWRITE_PRG_MAP_BUF
+#define REWRITE_PRG_MAP_BUF 1024
 #endif
 
 /* for better readbility */
@@ -253,6 +217,7 @@ typedef struct {
     const char *datafile;          /* filename for map data files         */
     const char *dbmtype;           /* dbm type for dbm map data files     */
     const char *checkfile;         /* filename to check for map existence */
+    const char *cachename;         /* for cached maps (txt/rnd/dbm)       */
     int   type;                    /* the type of the map                 */
     apr_file_t *fpin;              /* in  file pointer for program maps   */
     apr_file_t *fpout;             /* out file pointer for program maps   */
@@ -269,6 +234,7 @@ typedef enum {
     CONDPAT_FILE_SIZE,
     CONDPAT_FILE_LINK,
     CONDPAT_FILE_DIR,
+    CONDPAT_FILE_XBIT,
     CONDPAT_LU_URL,
     CONDPAT_LU_FILE,
     CONDPAT_STR_GT,
@@ -279,7 +245,7 @@ typedef enum {
 typedef struct {
     char        *input;   /* Input string of RewriteCond   */
     char        *pattern; /* the RegExp pattern string     */
-    regex_t     *regexp;  /* the precompiled regexp        */
+    ap_regex_t  *regexp;  /* the precompiled regexp        */
     int          flags;   /* Flags which control the match */
     pattern_type ptype;   /* pattern type                  */
 } rewritecond_entry;
@@ -293,11 +259,12 @@ typedef struct data_item {
 typedef struct {
     apr_array_header_t *rewriteconds;/* the corresponding RewriteCond entries */
     char      *pattern;              /* the RegExp pattern string             */
-    regex_t   *regexp;               /* the RegExp pattern compilation        */
+    ap_regex_t *regexp;              /* the RegExp pattern compilation        */
     char      *output;               /* the Substitution string               */
     int        flags;                /* Flags which control the substitution  */
     char      *forced_mimetype;      /* forced MIME type of substitution      */
-    int        forced_responsecode;  /* forced HTTP redirect response status  */
+    char      *forced_handler;       /* forced content handler of subst.      */
+    int        forced_responsecode;  /* forced HTTP response status           */
     data_item *env;                  /* added environment variables           */
     data_item *cookie;               /* added cookies                         */
     int        skip;                 /* number of next rules to skip          */
@@ -306,14 +273,15 @@ typedef struct {
 typedef struct {
     int           state;              /* the RewriteEngine state            */
     int           options;            /* the RewriteOption state            */
+#ifndef REWRITELOG_DISABLED
     const char   *rewritelogfile;     /* the RewriteLog filename            */
     apr_file_t   *rewritelogfp;       /* the RewriteLog open filepointer    */
     int           rewriteloglevel;    /* the RewriteLog level of verbosity  */
+#endif
     apr_hash_t         *rewritemaps;  /* the RewriteMap entries             */
     apr_array_header_t *rewriteconds; /* the RewriteCond entries (temp.)    */
     apr_array_header_t *rewriterules; /* the RewriteRule entries            */
     server_rec   *server;             /* the corresponding server indicator */
-    int          redirect_limit;      /* max number of internal redirects   */
 } rewrite_server_conf;
 
 typedef struct {
@@ -323,15 +291,8 @@ typedef struct {
     apr_array_header_t *rewriterules; /* the RewriteRule entries           */
     char         *directory;          /* the directory where it applies    */
     const char   *baseurl;            /* the base-URL  where it applies    */
-    int          redirect_limit;      /* max. number of internal redirects */
 } rewrite_perdir_conf;
 
-typedef struct {
-    int           redirects;      /* current number of redirects */
-    int           redirect_limit; /* maximum number of redirects */
-} rewrite_request_conf;
-
-
 /* the (per-child) cache structures.
  */
 typedef struct cache {
@@ -357,7 +318,7 @@ typedef struct {
 typedef struct backrefinfo {
     char *source;
     int nsub;
-    regmatch_t regmatch[10];
+    ap_regmatch_t regmatch[AP_MAX_REG_MATCH];
 } backrefinfo;
 
 /* single linked list used for
@@ -369,6 +330,17 @@ typedef struct result_list {
     const char *string;
 } result_list;
 
+/* context structure for variable lookup and expansion
+ */
+typedef struct {
+    request_rec *r;
+    const char  *uri;
+    const char  *vary_this;
+    const char  *vary;
+    char        *perdir;
+    backrefinfo briRR;
+    backrefinfo briRC;
+} rewrite_ctx;
 
 /*
  * +-------------------------------------------------------+
@@ -396,8 +368,14 @@ static int rewrite_rand_init_done = 0;
 /* Locks/Mutexes */
 static const char *lockname;
 static apr_global_mutex_t *rewrite_mapr_lock_acquire = NULL;
+
+#ifndef REWRITELOG_DISABLED
 static apr_global_mutex_t *rewrite_log_lock = NULL;
+#endif
 
+/* Optional functions imported from mod_ssl when loaded: */
+static APR_OPTIONAL_FN_TYPE(ssl_var_lookup) *rewrite_ssl_lookup = NULL;
+static APR_OPTIONAL_FN_TYPE(ssl_is_https) *rewrite_is_https = NULL;
 
 /*
  * +-------------------------------------------------------+
@@ -407,6 +385,7 @@ static apr_global_mutex_t *rewrite_log_lock = NULL;
  * +-------------------------------------------------------+
  */
 
+#ifndef REWRITELOG_DISABLED
 static char *current_logtime(request_rec *r)
 {
     apr_time_exp_t t;
@@ -481,7 +460,8 @@ static int open_rewritelog(server_rec *s, apr_pool_t *p)
     return 1;
 }
 
-static void rewritelog(request_rec *r, int level, const char *fmt, ...)
+static void do_rewritelog(request_rec *r, int level, char *perdir,
+                          const char *fmt, ...)
 {
     rewrite_server_conf *conf;
     char *logline, *text;
@@ -511,7 +491,7 @@ static void rewritelog(request_rec *r, int level, const char *fmt, ...)
     va_end(ap);
 
     logline = apr_psprintf(r->pool, "%s %s %s %s [%s/sid#%pp][rid#%pp/%s%s%s] "
-                                    "(%d) %s" APR_EOL_STR,
+                                    "(%d) %s%s%s%s" APR_EOL_STR,
                            rhost ? rhost : "UNKNOWN-HOST",
                            rname ? rname : "-",
                            r->user ? (*r->user ? r->user : "\"\"") : "-",
@@ -522,7 +502,11 @@ static void rewritelog(request_rec *r, int level, const char *fmt, ...)
                            r->main ? "subreq" : "initial",
                            redir ? "/redir#" : "",
                            redir ? apr_itoa(r->pool, redir) : "",
-                           level, text);
+                           level,
+                           perdir ? "[perdir " : "",
+                           perdir ? perdir : "",
+                           perdir ? "] ": "",
+                           text);
 
     rv = apr_global_mutex_lock(rewrite_log_lock);
     if (rv != APR_SUCCESS) {
@@ -543,6 +527,7 @@ static void rewritelog(request_rec *r, int level, const char *fmt, ...)
 
     return;
 }
+#endif /* !REWRITELOG_DISABLED */
 
 
 /*
@@ -574,6 +559,19 @@ static unsigned is_absolute_uri(char *uri)
     }
 
     switch (*uri++) {
+    case 'a':
+    case 'A':
+        if (!strncasecmp(uri, "jp://", 5)) {        /* ajp://    */
+          return 6;
+        }
+
+    case 'b':
+    case 'B':
+        if (!strncasecmp(uri, "alancer://", 10)) {   /* balancer:// */
+          return 11;
+        }
+        break;
+
     case 'f':
     case 'F':
         if (!strncasecmp(uri, "tp://", 5)) {        /* ftp://    */
@@ -707,7 +705,9 @@ static void splitout_queryargs(request_rec *r, int qsappend)
     /* don't touch, unless it's an http or mailto URL.
      * See RFC 1738 and RFC 2368.
      */
-    if (   is_absolute_uri(r->filename)
+    if (is_absolute_uri(r->filename)
+        && strncasecmp(r->filename, "ajp", 3)
+        && strncasecmp(r->filename, "balancer", 8)
         && strncasecmp(r->filename, "http", 4)
         && strncasecmp(r->filename, "mailto", 6)) {
         r->args = NULL; /* forget the query that's still flying around */
@@ -736,8 +736,8 @@ static void splitout_queryargs(request_rec *r, int qsappend)
             r->args[len-1] = '\0';
         }
 
-        rewritelog(r, 3, "split uri=%s -> uri=%s, args=%s", olduri,
-                   r->filename, r->args ? r->args : "<none>");
+        rewritelog((r, 3, NULL, "split uri=%s -> uri=%s, args=%s", olduri,
+                    r->filename, r->args ? r->args : "<none>"));
     }
 
     return;
@@ -751,7 +751,7 @@ static void reduce_uri(request_rec *r)
     char *cp;
     apr_size_t l;
 
-    cp = (char *)ap_http_method(r);
+    cp = (char *)ap_http_scheme(r);
     l  = strlen(cp);
     if (   strlen(r->filename) > l+3
         && strncasecmp(r->filename, cp, l) == 0
@@ -797,7 +797,7 @@ static void reduce_uri(request_rec *r)
 
         /* now check whether we could reduce it to a local path... */
         if (ap_matches_request_vhost(r, host, port)) {
-            rewritelog(r, 3, "reduce %s -> %s", r->filename, url);
+            rewritelog((r, 3, NULL, "reduce %s -> %s", r->filename, url));
             r->filename = apr_pstrdup(r->pool, url);
         }
     }
@@ -823,7 +823,7 @@ static void fully_qualify_uri(request_rec *r)
                    : apr_psprintf(r->pool, ":%u", port);
 
         r->filename = apr_psprintf(r->pool, "%s://%s%s%s%s",
-                                   ap_http_method(r), thisserver, thisport,
+                                   ap_http_scheme(r), thisserver, thisport,
                                    (*r->filename == '/') ? "" : "/",
                                    r->filename);
     }
@@ -891,7 +891,8 @@ static char *subst_prefix_path(request_rec *r, char *input, char *match,
         apr_size_t slen, outlen;
         char *output;
 
-        rewritelog(r, 5, "strip matching prefix: %s -> %s", input, input+len);
+        rewritelog((r, 5, NULL, "strip matching prefix: %s -> %s", input,
+                    input+len));
 
         slen = strlen(subst);
         if (slen && subst[slen - 1] != '/') {
@@ -908,7 +909,8 @@ static char *subst_prefix_path(request_rec *r, char *input, char *match,
         memcpy(output+slen, input+len, outlen - slen);
         output[outlen] = '\0';
 
-        rewritelog(r, 4, "add subst prefix: %s -> %s", input+len, output);
+        rewritelog((r, 4, NULL, "add subst prefix: %s -> %s", input+len,
+                    output));
 
         return output;
     }
@@ -1172,6 +1174,7 @@ static apr_status_t run_rewritemap_programs(server_rec *s, apr_pool_t *p)
     rewrite_server_conf *conf;
     apr_hash_index_t *hi;
     apr_status_t rc;
+    int lock_warning_issued = 0;
 
     conf = ap_get_module_config(s->module_config, &rewrite_module);
 
@@ -1198,6 +1201,13 @@ static apr_status_t run_rewritemap_programs(server_rec *s, apr_pool_t *p)
             continue;
         }
 
+        if (!lock_warning_issued && (!lockname || !*lockname)) {
+            ap_log_error(APLOG_MARK, APLOG_WARNING, 0, s,
+                         "mod_rewrite: Running external rewrite maps "
+                         "without defining a RewriteLock is DANGEROUS!");
+            ++lock_warning_issued;
+        }
+
         rc = rewritemap_program_child(p, map->argv[0], map->argv,
                                       &fpout, &fpin);
         if (rc != APR_SUCCESS || fpin == NULL || fpout == NULL) {
@@ -1309,11 +1319,14 @@ static char *lookup_map_dbmfile(request_rec *r, const char *file,
 static char *lookup_map_program(request_rec *r, apr_file_t *fpin,
                                 apr_file_t *fpout, char *key)
 {
-    char buf[REWRITE_MAX_PRG_MAP_LINE];
+    char *buf;
     char c;
-    apr_size_t i;
-    apr_size_t nbytes;
+    apr_size_t i, nbytes, combined_len = 0;
     apr_status_t rv;
+    const char *eol = APR_EOL_STR;
+    apr_size_t eolc = 0;
+    int found_nl = 0;
+    result_list *buflist = NULL, *curbuf = NULL;
 
 #ifndef NO_WRITEV
     struct iovec iova[2];
@@ -1361,17 +1374,90 @@ static char *lookup_map_program(request_rec *r, apr_file_t *fpin,
     apr_file_writev(fpin, iova, niov, &nbytes);
 #endif
 
+    buf = apr_palloc(r->pool, REWRITE_PRG_MAP_BUF + 1);
+
     /* read in the response value */
-    i = 0;
     nbytes = 1;
     apr_file_read(fpout, &c, &nbytes);
-    while (nbytes == 1 && (i < REWRITE_MAX_PRG_MAP_LINE)) {
-        if (c == '\n') {
-            break;
+    do {
+        i = 0;
+        while (nbytes == 1 && (i < REWRITE_PRG_MAP_BUF)) {
+            if (c == eol[eolc]) {
+                if (!eol[++eolc]) {
+                    /* remove eol from the buffer */
+                    --eolc;
+                    if (i < eolc) {
+                        curbuf->len -= eolc-i;
+                        i = 0;
+                    }
+                    else {
+                        i -= eolc;
+                    }
+                    ++found_nl;
+                    break;
+                }
+            }
+
+            /* only partial (invalid) eol sequence -> reset the counter */
+            else if (eolc) {
+                eolc = 0;
+            }
+
+            /* catch binary mode, e.g. on Win32 */
+            else if (c == '\n') {
+                ++found_nl;
+                break;
+            }
+
+            buf[i++] = c;
+            apr_file_read(fpout, &c, &nbytes);
+        }
+
+        /* well, if there wasn't a newline yet, we need to read further */
+        if (buflist || (nbytes == 1 && !found_nl)) {
+            if (!buflist) {
+                curbuf = buflist = apr_palloc(r->pool, sizeof(*buflist));
+            }
+            else if (i) {
+                curbuf->next = apr_palloc(r->pool, sizeof(*buflist));
+                curbuf = curbuf->next;
+                
+            }
+            curbuf->next = NULL;
+
+            if (i) {
+                curbuf->string = buf;
+                curbuf->len = i;
+                combined_len += i;
+                buf = apr_palloc(r->pool, REWRITE_PRG_MAP_BUF);
+            }
+
+            if (nbytes == 1 && !found_nl) {
+                i = 0;
+                continue;
+            }
         }
-        buf[i++] = c;
 
-        apr_file_read(fpout, &c, &nbytes);
+        break;
+    } while (1);
+
+    /* concat the stuff */
+    if (buflist) {
+        char *p;
+
+        p = buf = apr_palloc(r->pool, combined_len + 1); /* \0 */
+        while (buflist) {
+            if (buflist->len) {
+                memcpy(p, buflist->string, buflist->len);
+                p += buflist->len;
+            }
+            buflist = buflist->next;
+        }
+        *p = '\0';
+        i = combined_len;
+    }
+    else {
+        buf[i] = '\0';
     }
 
     /* give the lock back */
@@ -1385,11 +1471,12 @@ static char *lookup_map_program(request_rec *r, apr_file_t *fpin,
         }
     }
 
-    if (i == 4 && strncasecmp(buf, "NULL", 4) == 0) {
+    /* catch the "failed" case */
+    if (i == 4 && !strcasecmp(buf, "NULL")) {
         return NULL;
     }
 
-    return apr_pstrmemdup(r->pool, buf, i);
+    return buf;
 }
 
 /*
@@ -1423,34 +1510,36 @@ static char *lookup_map(request_rec *r, char *name, char *key)
             ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
                           "mod_rewrite: can't access text RewriteMap file %s",
                           s->checkfile);
-            rewritelog(r, 1, "can't open RewriteMap file, see error log");
+            rewritelog((r, 1, NULL,
+                        "can't open RewriteMap file, see error log"));
             return NULL;
         }
 
-        value = get_cache_value(name, st.mtime, key, r->pool);
+        value = get_cache_value(s->cachename, st.mtime, key, r->pool);
         if (!value) {
-            rewritelog(r, 6, "cache lookup FAILED, forcing new map lookup");
+            rewritelog((r, 6, NULL,
+                        "cache lookup FAILED, forcing new map lookup"));
 
             value = lookup_map_txtfile(r, s->datafile, key);
             if (!value) {
-                rewritelog(r, 5, "map lookup FAILED: map=%s[txt] key=%s",
-                           name, key);
-                set_cache_value(name, st.mtime, key, "");
+                rewritelog((r, 5, NULL, "map lookup FAILED: map=%s[txt] key=%s",
+                            name, key));
+                set_cache_value(s->cachename, st.mtime, key, "");
                 return NULL;
             }
 
-            rewritelog(r, 5, "map lookup OK: map=%s[txt] key=%s -> val=%s",
-                       name, key, value);
-            set_cache_value(name, st.mtime, key, value);
+            rewritelog((r, 5, NULL,"map lookup OK: map=%s[txt] key=%s -> val=%s",
+                        name, key, value));
+            set_cache_value(s->cachename, st.mtime, key, value);
         }
         else {
-            rewritelog(r, 5, "cache lookup OK: map=%s[txt] key=%s -> val=%s",
-                       name, key, value);
+            rewritelog((r,5,NULL,"cache lookup OK: map=%s[txt] key=%s -> val=%s",
+                        name, key, value));
         }
 
         if (s->type == MAPTYPE_RND && *value) {
             value = select_random_value_part(r, value);
-            rewritelog(r, 5, "randomly chosen the subvalue `%s'", value);
+            rewritelog((r, 5, NULL, "randomly chosen the subvalue `%s'",value));
         }
 
         return *value ? value : NULL;
@@ -1464,30 +1553,33 @@ static char *lookup_map(request_rec *r, char *name, char *key)
             ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
                           "mod_rewrite: can't access DBM RewriteMap file %s",
                           s->checkfile);
-            rewritelog(r, 1, "can't open DBM RewriteMap file, see error log");
+            rewritelog((r, 1, NULL,
+                        "can't open DBM RewriteMap file, see error log"));
             return NULL;
         }
 
-        value = get_cache_value(name, st.mtime, key, r->pool);
+        value = get_cache_value(s->cachename, st.mtime, key, r->pool);
         if (!value) {
-            rewritelog(r, 6, "cache lookup FAILED, forcing new map lookup");
+            rewritelog((r, 6, NULL,
+                        "cache lookup FAILED, forcing new map lookup"));
 
             value = lookup_map_dbmfile(r, s->datafile, s->dbmtype, key);
             if (!value) {
-                rewritelog(r, 5, "map lookup FAILED: map=%s[dbm] key=%s",
-                           name, key);
-                set_cache_value(name, st.mtime, key, "");
+                rewritelog((r, 5, NULL, "map lookup FAILED: map=%s[dbm] key=%s",
+                            name, key));
+                set_cache_value(s->cachename, st.mtime, key, "");
                 return NULL;
             }
 
-            rewritelog(r, 5, "map lookup OK: map=%s[dbm] key=%s -> val=%s",
-                       name, key, value);
-            set_cache_value(name, st.mtime, key, value);
+            rewritelog((r, 5, NULL, "map lookup OK: map=%s[dbm] key=%s -> "
+                        "val=%s", name, key, value));
+
+            set_cache_value(s->cachename, st.mtime, key, value);
             return value;
         }
 
-        rewritelog(r, 5, "cache lookup OK: map=%s[dbm] key=%s -> val=%s",
-                   name, key, value);
+        rewritelog((r, 5, NULL, "cache lookup OK: map=%s[dbm] key=%s -> val=%s",
+                    name, key, value));
         return *value ? value : NULL;
 
     /*
@@ -1496,12 +1588,13 @@ static char *lookup_map(request_rec *r, char *name, char *key)
     case MAPTYPE_PRG:
         value = lookup_map_program(r, s->fpin, s->fpout, key);
         if (!value) {
-            rewritelog(r, 5, "map lookup FAILED: map=%s key=%s", name, key);
+            rewritelog((r, 5,NULL,"map lookup FAILED: map=%s key=%s", name,
+                        key));
             return NULL;
         }
 
-        rewritelog(r, 5, "map lookup OK: map=%s key=%s -> val=%s",
-                   name, key, value);
+        rewritelog((r, 5, NULL, "map lookup OK: map=%s key=%s -> val=%s",
+                    name, key, value));
         return value;
 
     /*
@@ -1510,12 +1603,13 @@ static char *lookup_map(request_rec *r, char *name, char *key)
     case MAPTYPE_INT:
         value = s->func(r, key);
         if (!value) {
-            rewritelog(r, 5, "map lookup FAILED: map=%s key=%s", name, key);
+            rewritelog((r, 5,NULL,"map lookup FAILED: map=%s key=%s", name,
+                        key));
             return NULL;
         }
 
-        rewritelog(r, 5, "map lookup OK: map=%s key=%s -> val=%s",
-                   name, key, value);
+        rewritelog((r, 5, NULL, "map lookup OK: map=%s key=%s -> val=%s",
+                    name, key, value));
         return value;
     }
 
@@ -1525,23 +1619,46 @@ static char *lookup_map(request_rec *r, char *name, char *key)
 /*
  * lookup a HTTP header and set VARY note
  */
-static const char *lookup_header(request_rec *r, const char *name)
+static const char *lookup_header(const char *name, rewrite_ctx *ctx)
 {
-    const char *val = apr_table_get(r->headers_in, name);
+    const char *val = apr_table_get(ctx->r->headers_in, name);
 
     if (val) {
-        apr_table_merge(r->notes, VARY_KEY_THIS, name);
+        ctx->vary_this = ctx->vary_this
+                         ? apr_pstrcat(ctx->r->pool, ctx->vary_this, ", ",
+                                       name, NULL)
+                         : apr_pstrdup(ctx->r->pool, name);
     }
 
     return val;
 }
 
+/*
+ * lookahead helper function
+ * Determine the correct URI path in perdir context
+ */
+static APR_INLINE const char *la_u(rewrite_ctx *ctx)
+{
+    rewrite_perdir_conf *conf;
+
+    if (*ctx->uri == '/') {
+        return ctx->uri;
+    }
+
+    conf = ap_get_module_config(ctx->r->per_dir_config, &rewrite_module);
+
+    return apr_pstrcat(ctx->r->pool, conf->baseurl
+                                     ? conf->baseurl : conf->directory,
+                       ctx->uri, NULL);
+}
+
 /*
  * generic variable lookup
  */
-static char *lookup_variable(request_rec *r, char *var)
+static char *lookup_variable(char *var, rewrite_ctx *ctx)
 {
     const char *result;
+    request_rec *r = ctx->r;
     apr_size_t varlen = strlen(var);
 
     /* fast exit */
@@ -1564,34 +1681,65 @@ static char *lookup_variable(request_rec *r, char *var)
                 result = getenv(var);
             }
         }
+        else if (var[4] && !strncasecmp(var, "SSL", 3) && rewrite_ssl_lookup) {
+            result = rewrite_ssl_lookup(r->pool, r->server, r->connection, r,
+                                        var + 4);
+        }
     }
     else if (var[4] == ':') {
         if (var[5]) {
             request_rec *rr;
+            const char *path;
 
             if (!strncasecmp(var, "HTTP", 4)) {
-                result = lookup_header(r, var+5);
+                result = lookup_header(var+5, ctx);
             }
             else if (!strncasecmp(var, "LA-U", 4)) {
-                if (r->filename && subreq_ok(r)) {
-                    rr = ap_sub_req_lookup_uri(r->filename, r, NULL);
-                    result = apr_pstrdup(r->pool, lookup_variable(rr, var+5));
+                if (ctx->uri && subreq_ok(r)) {
+                    path = ctx->perdir ? la_u(ctx) : ctx->uri;
+                    rr = ap_sub_req_lookup_uri(path, r, NULL);
+                    ctx->r = rr;
+                    result = apr_pstrdup(r->pool, lookup_variable(var+5, ctx));
+                    ctx->r = r;
                     ap_destroy_sub_req(rr);
 
-                    rewritelog(r, 5, "lookahead: path=%s var=%s -> val=%s",
-                               r->filename, var+5, result);
+                    rewritelog((r, 5, ctx->perdir, "lookahead: path=%s var=%s "
+                                "-> val=%s", path, var+5, result));
 
                     return (char *)result;
                 }
             }
             else if (!strncasecmp(var, "LA-F", 4)) {
-                if (r->filename && subreq_ok(r)) {
-                    rr = ap_sub_req_lookup_file(r->filename, r, NULL);
-                    result = apr_pstrdup(r->pool, lookup_variable(rr, var+5));
+                if (ctx->uri && subreq_ok(r)) {
+                    path = ctx->uri;
+                    if (ctx->perdir && *path == '/') {
+                        /* sigh, the user wants a file based subrequest, but
+                         * we can't do one, since we don't know what the file
+                         * path is! In this case behave like LA-U.
+                         */
+                        rr = ap_sub_req_lookup_uri(path, r, NULL);
+                    }
+                    else {
+                        if (ctx->perdir) {
+                            rewrite_perdir_conf *conf;
+
+                            conf = ap_get_module_config(r->per_dir_config,
+                                                        &rewrite_module);
+
+                            path = apr_pstrcat(r->pool, conf->directory, path,
+                                               NULL);
+                        }
+
+                        rr = ap_sub_req_lookup_file(path, r, NULL);
+                    }
+
+                    ctx->r = rr;
+                    result = apr_pstrdup(r->pool, lookup_variable(var+5, ctx));
+                    ctx->r = r;
                     ap_destroy_sub_req(rr);
 
-                    rewritelog(r, 5, "lookahead: path=%s var=%s -> val=%s",
-                               r->filename, var+5, result);
+                    rewritelog((r, 5, ctx->perdir, "lookahead: path=%s var=%s "
+                                "-> val=%s", path, var+5, result));
 
                     return (char *)result;
                 }
@@ -1616,10 +1764,17 @@ static char *lookup_variable(request_rec *r, char *var)
                 result = apr_psprintf(r->pool, "%04d%02d%02d%02d%02d%02d",
                                       tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday,
                                       tm.tm_hour, tm.tm_min, tm.tm_sec);
-                rewritelog(r, 1, "RESULT='%s'", result);
+                rewritelog((r, 1, ctx->perdir, "RESULT='%s'", result));
                 return (char *)result;
             }
             break;
+            
+        case  5:
+            if (!strcmp(var, "HTTPS")) {
+                int flag = rewrite_is_https && rewrite_is_https(r->connection);
+                return apr_pstrdup(r->pool, flag ? "on" : "off");
+            }
+            break;
 
         case  8:
             switch (var[6]) {
@@ -1686,7 +1841,7 @@ static char *lookup_variable(request_rec *r, char *var)
 
             case 'S':
                 if (!strcmp(var, "HTTP_HOST")) {
-                    result = lookup_header(r, "Host");
+                    result = lookup_header("Host", ctx);
                 }
                 break;
 
@@ -1718,7 +1873,7 @@ static char *lookup_variable(request_rec *r, char *var)
 
             case 'E':
                 if (*var == 'H' && !strcmp(var, "HTTP_ACCEPT")) {
-                    result = lookup_header(r, "Accept");
+                    result = lookup_header("Accept", ctx);
                 }
                 else if (!strcmp(var, "THE_REQUEST")) {
                     result = r->the_request;
@@ -1735,17 +1890,20 @@ static char *lookup_variable(request_rec *r, char *var)
 
             case 'K':
                 if (!strcmp(var, "HTTP_COOKIE")) {
-                    result = lookup_header(r, "Cookie");
+                    result = lookup_header("Cookie", ctx);
                 }
                 break;
 
             case 'O':
-                if (*var == 'R' && !strcmp(var, "REMOTE_HOST")) {
+                if (*var == 'S' && !strcmp(var, "SERVER_PORT")) {
+                    return apr_psprintf(r->pool, "%u", ap_get_server_port(r));
+                }
+                else if (var[7] == 'H' && !strcmp(var, "REMOTE_HOST")) {
                     result = ap_get_remote_host(r->connection,r->per_dir_config,
                                                 REMOTE_NAME, NULL);
                 }
-                else if (!strcmp(var, "SERVER_PORT")) {
-                    return apr_psprintf(r->pool, "%u", ap_get_server_port(r));
+                else if (!strcmp(var, "REMOTE_PORT")) {
+                    return apr_itoa(r->pool, r->connection->remote_addr->port);
                 }
                 break;
 
@@ -1790,7 +1948,7 @@ static char *lookup_variable(request_rec *r, char *var)
 
             case 'P':
                 if (!strcmp(var, "HTTP_REFERER")) {
-                    result = lookup_header(r, "Referer");
+                    result = lookup_header("Referer", ctx);
                 }
                 break;
 
@@ -1816,7 +1974,7 @@ static char *lookup_variable(request_rec *r, char *var)
 
         case 14:
             if (*var == 'H' && !strcmp(var, "HTTP_FORWARDED")) {
-                result = lookup_header(r, "Forwarded");
+                result = lookup_header("Forwarded", ctx);
             }
             else if (!strcmp(var, "REQUEST_METHOD")) {
                 result = r->method;
@@ -1827,7 +1985,7 @@ static char *lookup_variable(request_rec *r, char *var)
             switch (var[7]) {
             case 'E':
                 if (!strcmp(var, "HTTP_USER_AGENT")) {
-                    result = lookup_header(r, "User-Agent");
+                    result = lookup_header("User-Agent", ctx);
                 }
                 break;
 
@@ -1859,7 +2017,7 @@ static char *lookup_variable(request_rec *r, char *var)
 
         case 21:
             if (!strcmp(var, "HTTP_PROXY_CONNECTION")) {
-                result = lookup_header(r, "Proxy-Connection");
+                result = lookup_header("Proxy-Connection", ctx);
             }
             break;
         }
@@ -1925,21 +2083,21 @@ static APR_INLINE char *find_char_in_curlies(char *s, int c)
  * are interpreted by a later expansion, producing results that
  * were not intended by the administrator.
  */
-static char *do_expand(request_rec *r, char *input,
-                       backrefinfo *briRR, backrefinfo *briRC)
+static char *do_expand(char *input, rewrite_ctx *ctx)
 {
     result_list *result, *current;
     result_list sresult[SMALL_EXPANSION];
     unsigned spc = 0;
     apr_size_t span, inputlen, outlen;
     char *p, *c;
+    apr_pool_t *pool = ctx->r->pool;
 
     span = strcspn(input, "\\$%");
     inputlen = strlen(input);
 
     /* fast exit */
     if (inputlen == span) {
-        return apr_pstrdup(r->pool, input);
+        return apr_pstrdup(pool, input);
     }
 
     /* well, actually something to do */
@@ -1957,7 +2115,8 @@ static char *do_expand(request_rec *r, char *input,
         if (current->len) {
             current->next = (spc < SMALL_EXPANSION)
                             ? &(sresult[spc++])
-                            : apr_palloc(r->pool, sizeof(result_list));
+                            : (result_list *)apr_palloc(pool,
+                                                        sizeof(result_list));
             current = current->next;
             current->next = NULL;
             current->len = 0;
@@ -1991,7 +2150,7 @@ static char *do_expand(request_rec *r, char *input,
 
             /* variable lookup */
             else if (*p == '%') {
-                p = lookup_variable(r, apr_pstrmemdup(r->pool, p+2, endp-p-2));
+                p = lookup_variable(apr_pstrmemdup(pool, p+2, endp-p-2), ctx);
 
                 span = strlen(p);
                 current->len = span;
@@ -2027,22 +2186,19 @@ static char *do_expand(request_rec *r, char *input,
                 else {
                     char *map, *dflt;
 
-                    map = apr_pstrmemdup(r->pool, p+2, endp-p-2);
+                    map = apr_pstrmemdup(pool, p+2, endp-p-2);
                     key = map + (key-p-2);
                     *key++ = '\0';
                     dflt = find_char_in_curlies(key, '|');
                     if (dflt) {
                         *dflt++ = '\0';
                     }
-                    else {
-                        dflt = "";
-                    }
 
                     /* reuse of key variable as result */
-                    key = lookup_map(r, map, do_expand(r, key, briRR, briRC));
+                    key = lookup_map(ctx->r, map, do_expand(key, ctx));
 
-                    if (!key && *dflt) {
-                        key = do_expand(r, dflt, briRR, briRC);
+                    if (!key && dflt && *dflt) {
+                        key = do_expand(dflt, ctx);
                     }
 
                     if (key) {
@@ -2060,10 +2216,10 @@ static char *do_expand(request_rec *r, char *input,
         /* backreference */
         else if (apr_isdigit(p[1])) {
             int n = p[1] - '0';
-            backrefinfo *bri = (*p == '$') ? briRR : briRC;
+            backrefinfo *bri = (*p == '$') ? &ctx->briRR : &ctx->briRC;
 
             /* see ap_pregsub() in server/util.c */
-            if (bri && n <= bri->nsub
+            if (bri->source && n < AP_MAX_REG_MATCH
                 && bri->regmatch[n].rm_eo > bri->regmatch[n].rm_so) {
                 span = bri->regmatch[n].rm_eo - bri->regmatch[n].rm_so;
 
@@ -2087,7 +2243,8 @@ static char *do_expand(request_rec *r, char *input,
             if (current->len) {
                 current->next = (spc < SMALL_EXPANSION)
                                 ? &(sresult[spc++])
-                                : apr_palloc(r->pool, sizeof(result_list));
+                                : (result_list *)apr_palloc(pool,
+                                                           sizeof(result_list));
                 current = current->next;
                 current->next = NULL;
             }
@@ -2101,7 +2258,7 @@ static char *do_expand(request_rec *r, char *input,
     } while (p < input+inputlen);
 
     /* assemble result */
-    c = p = apr_palloc(r->pool, outlen + 1); /* don't forget the \0 */
+    c = p = apr_palloc(pool, outlen + 1); /* don't forget the \0 */
     do {
         if (result->len) {
             ap_assert(c+result->len <= p+outlen); /* XXX: can be removed after
@@ -2122,18 +2279,18 @@ static char *do_expand(request_rec *r, char *input,
 /*
  * perform all the expansions on the environment variables
  */
-static void do_expand_env(request_rec *r, data_item *env,
-                          backrefinfo *briRR, backrefinfo *briRC)
+static void do_expand_env(data_item *env, rewrite_ctx *ctx)
 {
     char *name, *val;
 
     while (env) {
-        name = do_expand(r, env->data, briRR, briRC);
+        name = do_expand(env->data, ctx);
         if ((val = ap_strchr(name, ':')) != NULL) {
             *val++ = '\0';
 
-            apr_table_set(r->subprocess_env, name, val);
-            rewritelog(r, 5, "setting env variable '%s' to '%s'", name, val);
+            apr_table_set(ctx->r->subprocess_env, name, val);
+            rewritelog((ctx->r, 5, NULL, "setting env variable '%s' to '%s'",
+                        name, val));
         }
 
         env = env->next;
@@ -2174,40 +2331,49 @@ static void add_cookie(request_rec *r, char *s)
         notename = apr_pstrcat(rmain->pool, var, "_rewrite", NULL);
         apr_pool_userdata_get(&data, notename, rmain->pool);
         if (!data) {
+            char *exp_time = NULL;
+
             expires = apr_strtok(NULL, ":", &tok_cntx);
             path = expires ? apr_strtok(NULL, ":", &tok_cntx) : NULL;
 
+            if (expires) {
+                apr_time_exp_t tms;
+                apr_time_exp_gmt(&tms, r->request_time
+                                     + apr_time_from_sec((60 * atol(expires))));
+                exp_time = apr_psprintf(r->pool, "%s, %.2d-%s-%.4d "
+                                                 "%.2d:%.2d:%.2d GMT",
+                                        apr_day_snames[tms.tm_wday],
+                                        tms.tm_mday,
+                                        apr_month_snames[tms.tm_mon],
+                                        tms.tm_year+1900,
+                                        tms.tm_hour, tms.tm_min, tms.tm_sec);
+            }
+
             cookie = apr_pstrcat(rmain->pool,
                                  var, "=", val,
-                                 "; path=", (path)? path : "/",
+                                 "; path=", path ? path : "/",
                                  "; domain=", domain,
-                                 (expires)? "; expires=" : NULL,
-                                 (expires)?
-                                 ap_ht_time(r->pool,
-                                            r->request_time +
-                                            apr_time_from_sec((60 *
-                                                               atol(expires))),
-                                            "%a, %d-%b-%Y %T GMT", 1)
-                                          : NULL,
+                                 expires ? "; expires=" : NULL,
+                                 expires ? exp_time : NULL,
                                  NULL);
 
-            apr_table_add(rmain->err_headers_out, "Set-Cookie", cookie);
+            apr_table_addn(rmain->err_headers_out, "Set-Cookie", cookie);
             apr_pool_userdata_set("set", notename, NULL, rmain->pool);
-            rewritelog(rmain, 5, "setting cookie '%s'", cookie);
+            rewritelog((rmain, 5, NULL, "setting cookie '%s'", cookie));
         }
         else {
-            rewritelog(rmain, 5, "skipping already set cookie '%s'", var);
+            rewritelog((rmain, 5, NULL, "skipping already set cookie '%s'",
+                        var));
         }
     }
 
     return;
 }
 
-static void do_expand_cookie(request_rec *r, data_item *cookie,
-                             backrefinfo *briRR, backrefinfo *briRC)
+static void do_expand_cookie(data_item *cookie, rewrite_ctx *ctx)
 {
     while (cookie) {
-        add_cookie(r, do_expand(r, cookie->data, briRR, briRC));
+        add_cookie(ctx->r, do_expand(cookie->data, ctx));
         cookie = cookie->next;
     }
 
@@ -2282,7 +2448,7 @@ static apr_status_t rewritelock_create(server_rec *s, apr_pool_t *p)
         return rc;
     }
 
-#ifdef MOD_REWRITE_SET_MUTEX_PERMS
+#ifdef AP_NEED_SET_MUTEX_PERMS
     rc = unixd_set_global_mutex_perms(rewrite_mapr_lock_acquire);
     if (rc != APR_SUCCESS) {
         ap_log_error(APLOG_MARK, APLOG_CRIT, rc, s,
@@ -2417,14 +2583,15 @@ static void *config_server_create(apr_pool_t *p, server_rec *s)
 
     a->state           = ENGINE_DISABLED;
     a->options         = OPTION_NONE;
+#ifndef REWRITELOG_DISABLED
     a->rewritelogfile  = NULL;
     a->rewritelogfp    = NULL;
     a->rewriteloglevel = 0;
+#endif
     a->rewritemaps     = apr_hash_make(p);
     a->rewriteconds    = apr_array_make(p, 2, sizeof(rewritecond_entry));
     a->rewriterules    = apr_array_make(p, 2, sizeof(rewriterule_entry));
     a->server          = s;
-    a->redirect_limit  = 0; /* unset (use default) */
 
     return (void *)a;
 }
@@ -2441,15 +2608,13 @@ static void *config_server_merge(apr_pool_t *p, void *basev, void *overridesv)
     a->state   = overrides->state;
     a->options = overrides->options;
     a->server  = overrides->server;
-    a->redirect_limit = overrides->redirect_limit
-                          ? overrides->redirect_limit
-                          : base->redirect_limit;
 
     if (a->options & OPTION_INHERIT) {
         /*
          *  local directives override
          *  and anything else is inherited
          */
+#ifndef REWRITELOG_DISABLED
         a->rewriteloglevel = overrides->rewriteloglevel != 0
                              ? overrides->rewriteloglevel
                              : base->rewriteloglevel;
@@ -2459,6 +2624,7 @@ static void *config_server_merge(apr_pool_t *p, void *basev, void *overridesv)
         a->rewritelogfp    = overrides->rewritelogfp != NULL
                              ? overrides->rewritelogfp
                              : base->rewritelogfp;
+#endif
         a->rewritemaps     = apr_hash_overlay(p, overrides->rewritemaps,
                                               base->rewritemaps);
         a->rewriteconds    = apr_array_append(p, overrides->rewriteconds,
@@ -2471,9 +2637,11 @@ static void *config_server_merge(apr_pool_t *p, void *basev, void *overridesv)
          *  local directives override
          *  and anything else gets defaults
          */
+#ifndef REWRITELOG_DISABLED
         a->rewriteloglevel = overrides->rewriteloglevel;
         a->rewritelogfile  = overrides->rewritelogfile;
         a->rewritelogfp    = overrides->rewritelogfp;
+#endif
         a->rewritemaps     = overrides->rewritemaps;
         a->rewriteconds    = overrides->rewriteconds;
         a->rewriterules    = overrides->rewriterules;
@@ -2493,7 +2661,6 @@ static void *config_perdir_create(apr_pool_t *p, char *path)
     a->baseurl         = NULL;
     a->rewriteconds    = apr_array_make(p, 2, sizeof(rewritecond_entry));
     a->rewriterules    = apr_array_make(p, 2, sizeof(rewriterule_entry));
-    a->redirect_limit  = 0; /* unset (use server config) */
 
     if (path == NULL) {
         a->directory = NULL;
@@ -2524,9 +2691,6 @@ static void *config_perdir_merge(apr_pool_t *p, void *basev, void *overridesv)
     a->options   = overrides->options;
     a->directory = overrides->directory;
     a->baseurl   = overrides->baseurl;
-    a->redirect_limit = overrides->redirect_limit
-                          ? overrides->redirect_limit
-                          : base->redirect_limit;
 
     if (a->options & OPTION_INHERIT) {
         a->rewriteconds = apr_array_append(p, overrides->rewriteconds,
@@ -2563,7 +2727,7 @@ static const char *cmd_rewriteengine(cmd_parms *cmd,
 static const char *cmd_rewriteoptions(cmd_parms *cmd,
                                       void *in_dconf, const char *option)
 {
-    int options = 0, limit = 0;
+    int options = 0;
     char *w;
 
     while (*option) {
@@ -2573,15 +2737,11 @@ static const char *cmd_rewriteoptions(cmd_parms *cmd,
             options |= OPTION_INHERIT;
         }
         else if (!strncasecmp(w, "MaxRedirects=", 13)) {
-            limit = atoi(&w[13]);
-            if (limit <= 0) {
-                return "RewriteOptions: MaxRedirects takes a number greater "
-                       "than zero.";
-            }
-        }
-        else if (!strcasecmp(w, "MaxRedirects")) { /* be nice */
-            return "RewriteOptions: MaxRedirects has the format MaxRedirects"
-                   "=n.";
+            ap_log_error(APLOG_MARK, APLOG_WARNING, 0, cmd->server,
+                         "RewriteOptions: MaxRedirects option has been "
+                         "removed in favor of the global "
+                         "LimitInternalRecursion directive and will be "
+                         "ignored.");
         }
         else {
             return apr_pstrcat(cmd->pool, "RewriteOptions: unknown option '",
@@ -2596,18 +2756,17 @@ static const char *cmd_rewriteoptions(cmd_parms *cmd,
                                  &rewrite_module);
 
         conf->options |= options;
-        conf->redirect_limit = limit;
     }
     else {                  /* is per-directory command */
         rewrite_perdir_conf *conf = in_dconf;
 
         conf->options |= options;
-        conf->redirect_limit = limit;
     }
 
     return NULL;
 }
 
+#ifndef REWRITELOG_DISABLED
 static const char *cmd_rewritelog(cmd_parms *cmd, void *dconf, const char *a1)
 {
     rewrite_server_conf *sconf;
@@ -2628,6 +2787,7 @@ static const char *cmd_rewriteloglevel(cmd_parms *cmd, void *dconf,
 
     return NULL;
 }
+#endif /* rewritelog */
 
 static const char *cmd_rewritemap(cmd_parms *cmd, void *dconf, const char *a1,
                                   const char *a2)
@@ -2651,6 +2811,8 @@ static const char *cmd_rewritemap(cmd_parms *cmd, void *dconf, const char *a1,
         newmap->type      = MAPTYPE_TXT;
         newmap->datafile  = fname;
         newmap->checkfile = fname;
+        newmap->cachename = apr_psprintf(cmd->pool, "%pp:%s",
+                                         (void *)cmd->server, a1);
     }
     else if (strncasecmp(a2, "rnd:", 4) == 0) {
         if ((fname = ap_server_root_relative(cmd->pool, a2+4)) == NULL) {
@@ -2661,6 +2823,8 @@ static const char *cmd_rewritemap(cmd_parms *cmd, void *dconf, const char *a1,
         newmap->type      = MAPTYPE_RND;
         newmap->datafile  = fname;
         newmap->checkfile = fname;
+        newmap->cachename = apr_psprintf(cmd->pool, "%pp:%s",
+                                         (void *)cmd->server, a1);
     }
     else if (strncasecmp(a2, "dbm", 3) == 0) {
         const char *ignored_fname;
@@ -2668,6 +2832,8 @@ static const char *cmd_rewritemap(cmd_parms *cmd, void *dconf, const char *a1,
 
         newmap->type = MAPTYPE_DBM;
         fname = NULL;
+        newmap->cachename = apr_psprintf(cmd->pool, "%pp:%s",
+                                         (void *)cmd->server, a1);
 
         if (a2[3] == ':') {
             newmap->dbmtype = "default";
@@ -2715,11 +2881,13 @@ static const char *cmd_rewritemap(cmd_parms *cmd, void *dconf, const char *a1,
         newmap->type      = MAPTYPE_PRG;
         newmap->datafile  = NULL;
         newmap->checkfile = newmap->argv[0];
+        newmap->cachename = NULL;
     }
     else if (strncasecmp(a2, "int:", 4) == 0) {
         newmap->type      = MAPTYPE_INT;
         newmap->datafile  = NULL;
         newmap->checkfile = NULL;
+        newmap->cachename = NULL;
         newmap->func      = (char *(*)(request_rec *,char *))
                             apr_hash_get(mapfunc_hash, a2+4, strlen(a2+4));
         if ((sconf->state == ENGINE_ENABLED) && (newmap->func == NULL)) {
@@ -2736,6 +2904,8 @@ static const char *cmd_rewritemap(cmd_parms *cmd, void *dconf, const char *a1,
         newmap->type      = MAPTYPE_TXT;
         newmap->datafile  = fname;
         newmap->checkfile = fname;
+        newmap->cachename = apr_psprintf(cmd->pool, "%pp:%s",
+                                         (void *)cmd->server, a1);
     }
     newmap->fpin  = NULL;
     newmap->fpout = NULL;
@@ -2876,7 +3046,7 @@ static const char *cmd_rewritecond(cmd_parms *cmd, void *in_dconf,
     char *str = apr_pstrdup(cmd->pool, in_str);
     rewrite_server_conf *sconf;
     rewritecond_entry *newcond;
-    regex_t *regexp;
+    ap_regex_t *regexp;
     char *a1;
     char *a2;
     char *a3;
@@ -2932,6 +3102,7 @@ static const char *cmd_rewritecond(cmd_parms *cmd, void *in_dconf,
             case 's': newcond->ptype = CONDPAT_FILE_SIZE;   break;
             case 'l': newcond->ptype = CONDPAT_FILE_LINK;   break;
             case 'd': newcond->ptype = CONDPAT_FILE_DIR;    break;
+            case 'x': newcond->ptype = CONDPAT_FILE_XBIT;   break;
             case 'U': newcond->ptype = CONDPAT_LU_URL;      break;
             case 'F': newcond->ptype = CONDPAT_LU_FILE;     break;
             }
@@ -2962,8 +3133,8 @@ static const char *cmd_rewritecond(cmd_parms *cmd, void *in_dconf,
 
     if (!newcond->ptype) {
         regexp = ap_pregcomp(cmd->pool, a2,
-                             REG_EXTENDED | ((newcond->flags & CONDFLAG_NOCASE)
-                                             ? REG_ICASE : 0));
+                             AP_REG_EXTENDED | ((newcond->flags & CONDFLAG_NOCASE)
+                                             ? AP_REG_ICASE : 0));
         if (!regexp) {
             return apr_pstrcat(cmd->pool, "RewriteCond: cannot compile regular "
                                "expression '", a2, "'", NULL);
@@ -2979,7 +3150,7 @@ static const char *cmd_rewriterule_setflag(apr_pool_t *p, void *_cfg,
                                            char *key, char *val)
 {
     rewriterule_entry *cfg = _cfg;
-    int status = 0;
+    int error = 0;
 
     switch (*key++) {
     case 'c':
@@ -3005,6 +3176,9 @@ static const char *cmd_rewriterule_setflag(apr_pool_t *p, void *_cfg,
             cp->next = NULL;
             cp->data = val;
         }
+        else {
+            ++error;
+        }
         break;
 
     case 'e':
@@ -3026,19 +3200,40 @@ static const char *cmd_rewriterule_setflag(apr_pool_t *p, void *_cfg,
             cp->next = NULL;
             cp->data = val;
         }
+        else {
+            ++error;
+        }
         break;
 
     case 'f':
     case 'F':
         if (!*key || !strcasecmp(key, "orbidden")) {       /* forbidden */
-            cfg->flags |= RULEFLAG_FORBIDDEN;
+            cfg->flags |= (RULEFLAG_STATUS | RULEFLAG_NOSUB);
+            cfg->forced_responsecode = HTTP_FORBIDDEN;
+        }
+        else {
+            ++error;
         }
         break;
 
     case 'g':
     case 'G':
         if (!*key || !strcasecmp(key, "one")) {            /* gone */
-            cfg->flags |= RULEFLAG_GONE;
+            cfg->flags |= (RULEFLAG_STATUS | RULEFLAG_NOSUB);
+            cfg->forced_responsecode = HTTP_GONE;
+        }
+        else {
+            ++error;
+        }
+        break;
+
+    case 'h':
+    case 'H':
+        if (!*key || !strcasecmp(key, "andler")) {         /* handler */
+            cfg->forced_handler = val;
+        }
+        else {
+            ++error;
         }
         break;
 
@@ -3047,6 +3242,9 @@ static const char *cmd_rewriterule_setflag(apr_pool_t *p, void *_cfg,
         if (!*key || !strcasecmp(key, "ast")) {            /* last */
             cfg->flags |= RULEFLAG_LASTRULE;
         }
+        else {
+            ++error;
+        }
         break;
 
     case 'n':
@@ -3066,6 +3264,9 @@ static const char *cmd_rewriterule_setflag(apr_pool_t *p, void *_cfg,
             || !strcasecmp(key, "ocase")) {                /* nocase */
             cfg->flags |= RULEFLAG_NOCASE;
         }
+        else {
+            ++error;
+        }
         break;
 
     case 'p':
@@ -3077,19 +3278,27 @@ static const char *cmd_rewriterule_setflag(apr_pool_t *p, void *_cfg,
             || !strcasecmp(key, "assthrough")) {           /* passthrough */
             cfg->flags |= RULEFLAG_PASSTHROUGH;
         }
+        else {
+            ++error;
+        }
         break;
 
     case 'q':
     case 'Q':
-        if (   !strcasecmp(key, "QSA")
-            || !strcasecmp(key, "qsappend")) {             /* qsappend */
+        if (   !strcasecmp(key, "SA")
+            || !strcasecmp(key, "sappend")) {              /* qsappend */
             cfg->flags |= RULEFLAG_QSAPPEND;
         }
+        else {
+            ++error;
+        }
         break;
 
     case 'r':
     case 'R':
         if (!*key || !strcasecmp(key, "edirect")) {        /* redirect */
+            int status = 0;
+
             cfg->flags |= RULEFLAG_FORCEREDIRECT;
             if (strlen(val) > 0) {
                 if (strcasecmp(val, "permanent") == 0) {
@@ -3103,14 +3312,27 @@ static const char *cmd_rewriterule_setflag(apr_pool_t *p, void *_cfg,
                 }
                 else if (apr_isdigit(*val)) {
                     status = atoi(val);
+                    if (status != HTTP_INTERNAL_SERVER_ERROR) {
+                        int idx =
+                            ap_index_of_response(HTTP_INTERNAL_SERVER_ERROR);
+
+                        if (ap_index_of_response(status) == idx) {
+                            return apr_psprintf(p, "RewriteRule: invalid HTTP "
+                                                   "response code '%s' for "
+                                                   "flag 'R'",
+                                                val);
+                        }
+                    }
                     if (!ap_is_HTTP_REDIRECT(status)) {
-                        return "RewriteRule: invalid HTTP response code "
-                               "for flag 'R'";
+                        cfg->flags |= (RULEFLAG_STATUS | RULEFLAG_NOSUB);
                     }
                 }
                 cfg->forced_responsecode = status;
             }
         }
+        else {
+            ++error;
+        }
         break;
 
     case 's':
@@ -3118,18 +3340,28 @@ static const char *cmd_rewriterule_setflag(apr_pool_t *p, void *_cfg,
         if (!*key || !strcasecmp(key, "kip")) {            /* skip */
             cfg->skip = atoi(val);
         }
+        else {
+            ++error;
+        }
         break;
 
     case 't':
     case 'T':
         if (!*key || !strcasecmp(key, "ype")) {            /* type */
             cfg->forced_mimetype = val;
-            ap_str_tolower(cfg->forced_mimetype);
+        }
+        else {
+            ++error;
         }
         break;
 
     default:
-        return apr_pstrcat(p, "RewriteRule: unknown flag '", key, "'", NULL);
+        ++error;
+        break;
+    }
+
+    if (error) {
+        return apr_pstrcat(p, "RewriteRule: unknown flag '", --key, "'", NULL);
     }
 
     return NULL;
@@ -3142,7 +3374,7 @@ static const char *cmd_rewriterule(cmd_parms *cmd, void *in_dconf,
     char *str = apr_pstrdup(cmd->pool, in_str);
     rewrite_server_conf *sconf;
     rewriterule_entry *newrule;
-    regex_t *regexp;
+    ap_regex_t *regexp;
     char *a1;
     char *a2;
     char *a3;
@@ -3166,6 +3398,7 @@ static const char *cmd_rewriterule(cmd_parms *cmd, void *in_dconf,
 
     /* arg3: optional flags field */
     newrule->forced_mimetype     = NULL;
+    newrule->forced_handler      = NULL;
     newrule->forced_responsecode = HTTP_MOVED_TEMPORARILY;
     newrule->flags  = RULEFLAG_NONE;
     newrule->env = NULL;
@@ -3186,9 +3419,9 @@ static const char *cmd_rewriterule(cmd_parms *cmd, void *in_dconf,
         ++a1;
     }
 
-    regexp = ap_pregcomp(cmd->pool, a1, REG_EXTENDED |
+    regexp = ap_pregcomp(cmd->pool, a1, AP_REG_EXTENDED |
                                         ((newrule->flags & RULEFLAG_NOCASE)
-                                         ? REG_ICASE : 0));
+                                         ? AP_REG_ICASE : 0));
     if (!regexp) {
         return apr_pstrcat(cmd->pool,
                            "RewriteRule: cannot compile regular expression '",
@@ -3200,6 +3433,9 @@ static const char *cmd_rewriterule(cmd_parms *cmd, void *in_dconf,
 
     /* arg2: the output string */
     newrule->output = a2;
+    if (*a2 == '-' && !a2[1]) {
+        newrule->flags |= RULEFLAG_NOSUB;
+    }
 
     /* now, if the server or per-dir config holds an
      * array of RewriteCond entries, we take it for us
@@ -3252,14 +3488,12 @@ static APR_INLINE int compare_lexicography(char *a, char *b)
 /*
  * Apply a single rewriteCond
  */
-static int apply_rewrite_cond(request_rec *r, rewritecond_entry *p,
-                              char *perdir, backrefinfo *briRR,
-                              backrefinfo *briRC)
+static int apply_rewrite_cond(rewritecond_entry *p, rewrite_ctx *ctx)
 {
-    char *input = do_expand(r, p->input, briRR, briRC);
+    char *input = do_expand(p->input, ctx);
     apr_finfo_t sb;
-    request_rec *rsub;
-    regmatch_t regmatch[MAX_NMATCH];
+    request_rec *rsub, *r = ctx->r;
+    ap_regmatch_t regmatch[AP_MAX_REG_MATCH];
     int rc = 0;
 
     switch (p->ptype) {
@@ -3279,7 +3513,8 @@ static int apply_rewrite_cond(request_rec *r, rewritecond_entry *p,
 
     case CONDPAT_FILE_LINK:
 #if !defined(OS2)
-        if (   apr_lstat(&sb, input, APR_FINFO_MIN, r->pool) == APR_SUCCESS
+        if (   apr_stat(&sb, input, APR_FINFO_MIN | APR_FINFO_LINK,
+                        r->pool) == APR_SUCCESS
             && sb.filetype == APR_LNK) {
             rc = 1;
         }
@@ -3293,14 +3528,21 @@ static int apply_rewrite_cond(request_rec *r, rewritecond_entry *p,
         }
         break;
 
+    case CONDPAT_FILE_XBIT:
+        if (   apr_stat(&sb, input, APR_FINFO_PROT, r->pool) == APR_SUCCESS
+            && (sb.protection & (APR_UEXECUTE | APR_GEXECUTE | APR_WEXECUTE))) {
+            rc = 1;
+        }
+        break;
+
     case CONDPAT_LU_URL:
         if (*input && subreq_ok(r)) {
             rsub = ap_sub_req_lookup_uri(input, r, NULL);
             if (rsub->status < 400) {
                 rc = 1;
             }
-            rewritelog(r, 5, "RewriteCond URI (-U) check: "
-                       "path=%s -> status=%d", input, rsub->status);
+            rewritelog((r, 5, NULL, "RewriteCond URI (-U) check: "
+                        "path=%s -> status=%d", input, rsub->status));
             ap_destroy_sub_req(rsub);
         }
         break;
@@ -3314,9 +3556,9 @@ static int apply_rewrite_cond(request_rec *r, rewritecond_entry *p,
                          r->pool) == APR_SUCCESS) {
                 rc = 1;
             }
-            rewritelog(r, 5, "RewriteCond file (-F) check: path=%s "
-                       "-> file=%s status=%d", input, rsub->filename,
-                       rsub->status);
+            rewritelog((r, 5, NULL, "RewriteCond file (-F) check: path=%s "
+                        "-> file=%s status=%d", input, rsub->filename,
+                        rsub->status));
             ap_destroy_sub_req(rsub);
         }
         break;
@@ -3340,13 +3582,13 @@ static int apply_rewrite_cond(request_rec *r, rewritecond_entry *p,
 
     default:
         /* it is really a regexp pattern, so apply it */
-        rc = !ap_regexec(p->regexp, input, p->regexp->re_nsub+1, regmatch, 0);
+        rc = !ap_regexec(p->regexp, input, AP_MAX_REG_MATCH, regmatch, 0);
 
         /* update briRC backref info */
         if (rc && !(p->flags & CONDFLAG_NOTMATCH)) {
-            briRC->source = input;
-            briRC->nsub   = p->regexp->re_nsub;
-            memcpy(briRC->regmatch, regmatch, sizeof(regmatch));
+            ctx->briRC.source = input;
+            ctx->briRC.nsub   = p->regexp->re_nsub;
+            memcpy(ctx->briRC.regmatch, regmatch, sizeof(regmatch));
         }
         break;
     }
@@ -3355,368 +3597,266 @@ static int apply_rewrite_cond(request_rec *r, rewritecond_entry *p,
         rc = !rc;
     }
 
-    rewritelog(r, 4, "RewriteCond: input='%s' pattern='%s%s%s'%s => %s",
-               input, (p->flags & CONDFLAG_NOTMATCH) ? "!" : "",
-               (p->ptype == CONDPAT_STR_EQ) ? "=" : "", p->pattern,
-               (p->flags & CONDFLAG_NOCASE) ? " [NC]" : "",
-               rc ? "matched" : "not-matched");
+    rewritelog((r, 4, ctx->perdir, "RewriteCond: input='%s' pattern='%s%s%s'%s "
+                "=> %s", input, (p->flags & CONDFLAG_NOTMATCH) ? "!" : "",
+                (p->ptype == CONDPAT_STR_EQ) ? "=" : "", p->pattern,
+                (p->flags & CONDFLAG_NOCASE) ? " [NC]" : "",
+                rc ? "matched" : "not-matched"));
 
     return rc;
 }
 
+/* check for forced type and handler */
+static APR_INLINE void force_type_handler(rewriterule_entry *p,
+                                          rewrite_ctx *ctx)
+{
+    char *expanded;
+
+    if (p->forced_mimetype) {
+        expanded = do_expand(p->forced_mimetype, ctx);
+
+        if (*expanded) {
+            ap_str_tolower(expanded);
+
+            rewritelog((ctx->r, 2, ctx->perdir, "remember %s to have MIME-type "
+                        "'%s'", ctx->r->filename, expanded));
+
+            apr_table_setn(ctx->r->notes, REWRITE_FORCED_MIMETYPE_NOTEVAR,
+                           expanded);
+        }
+    }
+
+    if (p->forced_handler) {
+        expanded = do_expand(p->forced_handler, ctx);
+
+        if (*expanded) {
+            ap_str_tolower(expanded);
+
+            rewritelog((ctx->r, 2, ctx->perdir, "remember %s to have "
+                        "Content-handler '%s'", ctx->r->filename, expanded));
+
+            apr_table_setn(ctx->r->notes, REWRITE_FORCED_HANDLER_NOTEVAR,
+                           expanded);
+        }
+    }
+}
+
 /*
- *  Apply a single RewriteRule
+ * Apply a single RewriteRule
  */
-static int apply_rewrite_rule(request_rec *r, rewriterule_entry *p,
-                              char *perdir)
+static int apply_rewrite_rule(rewriterule_entry *p, rewrite_ctx *ctx)
 {
-    char *uri;
-    char *output;
-    const char *vary;
-    char *newuri;
-    regex_t *regexp;
-    regmatch_t regmatch[MAX_NMATCH];
-    backrefinfo *briRR = NULL;
-    backrefinfo *briRC = NULL;
-    int prefixstrip;
-    int failed;
+    ap_regmatch_t regmatch[AP_MAX_REG_MATCH];
     apr_array_header_t *rewriteconds;
     rewritecond_entry *conds;
-    rewritecond_entry *c;
-    int i;
-    int rc;
+    int i, rc;
+    char *newuri = NULL;
+    request_rec *r = ctx->r;
+    int is_proxyreq = 0;
 
-    /*
-     *  Initialisation
-     */
-    uri     = r->filename;
-    regexp  = p->regexp;
-    output  = p->output;
+    ctx->uri = r->filename;
 
-    /*
-     *  Add (perhaps splitted away) PATH_INFO postfix to URL to
-     *  make sure we really match against the complete URL.
-     */
-    if (perdir != NULL && r->path_info != NULL && r->path_info[0] != '\0') {
-        rewritelog(r, 3, "[per-dir %s] add path info postfix: %s -> %s%s",
-                   perdir, uri, uri, r->path_info);
-        uri = apr_pstrcat(r->pool, uri, r->path_info, NULL);
-    }
+    if (ctx->perdir) {
+        apr_size_t dirlen = strlen(ctx->perdir);
 
-    /*
-     *  On per-directory context (.htaccess) strip the location
-     *  prefix from the URL to make sure patterns apply only to
-     *  the local part.  Additionally indicate this special
-     *  threatment in the logfile.
-     */
-    prefixstrip = 0;
-    if (perdir != NULL) {
-        if (   strlen(uri) >= strlen(perdir)
-            && strncmp(uri, perdir, strlen(perdir)) == 0) {
-            rewritelog(r, 3, "[per-dir %s] strip per-dir prefix: %s -> %s",
-                       perdir, uri, uri+strlen(perdir));
-            uri = uri+strlen(perdir);
-            prefixstrip = 1;
+        /*
+         * Proxy request?
+         */
+        is_proxyreq = (   r->proxyreq && r->filename
+                       && !strncmp(r->filename, "proxy:", 6));
+
+        /* Since we want to match against the (so called) full URL, we have
+         * to re-add the PATH_INFO postfix
+         */
+        if (r->path_info && *r->path_info) {
+            rewritelog((r, 3, ctx->perdir, "add path info postfix: %s -> %s%s",
+                        ctx->uri, ctx->uri, r->path_info));
+            ctx->uri = apr_pstrcat(r->pool, ctx->uri, r->path_info, NULL);
+        }
+
+        /* Additionally we strip the physical path from the url to match
+         * it independent from the underlaying filesystem.
+         */
+        if (!is_proxyreq && strlen(ctx->uri) >= dirlen &&
+            !strncmp(ctx->uri, ctx->perdir, dirlen)) {
+
+            rewritelog((r, 3, ctx->perdir, "strip per-dir prefix: %s -> %s",
+                        ctx->uri, ctx->uri + dirlen));
+            ctx->uri = ctx->uri + dirlen;
         }
     }
 
-    /*
-     *  Try to match the URI against the RewriteRule pattern
-     *  and exit immeddiately if it didn't apply.
+    /* Try to match the URI against the RewriteRule pattern
+     * and exit immediately if it didn't apply.
      */
-    if (perdir == NULL) {
-        rewritelog(r, 3, "applying pattern '%s' to uri '%s'",
-                   p->pattern, uri);
-    }
-    else {
-        rewritelog(r, 3, "[per-dir %s] applying pattern '%s' to uri '%s'",
-                   perdir, p->pattern, uri);
-    }
-    rc = (ap_regexec(regexp, uri, regexp->re_nsub+1, regmatch, 0) == 0);
+    rewritelog((r, 3, ctx->perdir, "applying pattern '%s' to uri '%s'",
+                p->pattern, ctx->uri));
+
+    rc = !ap_regexec(p->regexp, ctx->uri, AP_MAX_REG_MATCH, regmatch, 0);
     if (! (( rc && !(p->flags & RULEFLAG_NOTMATCH)) ||
            (!rc &&  (p->flags & RULEFLAG_NOTMATCH))   ) ) {
         return 0;
     }
 
-    /*
-     *  Else create the RewriteRule `regsubinfo' structure which
-     *  holds the substitution information.
+    /* It matched, wow! Now it's time to prepare the context structure for
+     * further processing
      */
-    briRR = (backrefinfo *)apr_palloc(r->pool, sizeof(backrefinfo));
-    if (!rc && (p->flags & RULEFLAG_NOTMATCH)) {
-        /*  empty info on negative patterns  */
-        briRR->source = "";
-        briRR->nsub   = 0;
+    ctx->vary_this = NULL;
+    ctx->briRC.source = NULL;
+
+    if (p->flags & RULEFLAG_NOTMATCH) {
+        ctx->briRR.source = NULL;
     }
     else {
-        briRR->source = apr_pstrdup(r->pool, uri);
-        briRR->nsub   = regexp->re_nsub;
-        memcpy((void *)(briRR->regmatch), (void *)(regmatch),
-               sizeof(regmatch));
+        ctx->briRR.source = apr_pstrdup(r->pool, ctx->uri);
+        ctx->briRR.nsub   = p->regexp->re_nsub;
+        memcpy(ctx->briRR.regmatch, regmatch, sizeof(regmatch));
     }
 
-    /*
-     *  Initiallally create the RewriteCond backrefinfo with
-     *  empty backrefinfo, i.e. not subst parts
-     *  (this one is adjusted inside apply_rewrite_cond() later!!)
-     */
-    briRC = (backrefinfo *)apr_pcalloc(r->pool, sizeof(backrefinfo));
-    briRC->source = "";
-    briRC->nsub   = 0;
-
-    /*
-     *  Ok, we already know the pattern has matched, but we now
-     *  additionally have to check for all existing preconditions
-     *  (RewriteCond) which have to be also true. We do this at
-     *  this very late stage to avoid unnessesary checks which
-     *  would slow down the rewriting engine!!
+    /* Ok, we already know the pattern has matched, but we now
+     * additionally have to check for all existing preconditions
+     * (RewriteCond) which have to be also true. We do this at
+     * this very late stage to avoid unnessesary checks which
+     * would slow down the rewriting engine.
      */
     rewriteconds = p->rewriteconds;
     conds = (rewritecond_entry *)rewriteconds->elts;
-    failed = 0;
-    for (i = 0; i < rewriteconds->nelts; i++) {
-        c = &conds[i];
-        rc = apply_rewrite_cond(r, c, perdir, briRR, briRC);
+
+    for (i = 0; i < rewriteconds->nelts; ++i) {
+        rewritecond_entry *c = &conds[i];
+
+        rc = apply_rewrite_cond(c, ctx);
         if (c->flags & CONDFLAG_ORNEXT) {
-            /*
-             *  The "OR" case
-             */
-            if (rc == 0) {
-                /*  One condition is false, but another can be
-                 *  still true, so we have to continue...
-                 */
-                apr_table_unset(r->notes, VARY_KEY_THIS);
+            if (!rc) {
+                /* One condition is false, but another can be still true. */
+                ctx->vary_this = NULL;
                 continue;
             }
             else {
-                /*  One true condition is enough in "or" case, so
-                 *  skip the other conditions which are "ornext"
-                 *  chained
-                 */
+                /* skip the rest of the chained OR conditions */
                 while (   i < rewriteconds->nelts
                        && c->flags & CONDFLAG_ORNEXT) {
-                    i++;
-                    c = &conds[i];
+                    c = &conds[++i];
                 }
                 continue;
             }
         }
-        else {
-            /*
-             *  The "AND" case, i.e. no "or" flag,
-             *  so a single failure means total failure.
-             */
-            if (rc == 0) {
-                failed = 1;
-                break;
-            }
+        else if (!rc) {
+            return 0;
         }
-        vary = apr_table_get(r->notes, VARY_KEY_THIS);
-        if (vary != NULL) {
-            apr_table_merge(r->notes, VARY_KEY, vary);
-            apr_table_unset(r->notes, VARY_KEY_THIS);
+
+        /* If some HTTP header was involved in the condition, remember it
+         * for later use
+         */
+        if (ctx->vary_this) {
+            ctx->vary = ctx->vary
+                        ? apr_pstrcat(r->pool, ctx->vary, ", ", ctx->vary_this,
+                                      NULL)
+                        : ctx->vary_this;
+            ctx->vary_this = NULL;
         }
     }
-    /*  if any condition fails the complete rule fails  */
-    if (failed) {
-        apr_table_unset(r->notes, VARY_KEY);
-        apr_table_unset(r->notes, VARY_KEY_THIS);
-        return 0;
-    }
 
-    /*
-     * Regardless of what we do next, we've found a match.  Check to see
-     * if any of the request header fields were involved, and add them
-     * to the Vary field of the response.
-     */
-    if ((vary = apr_table_get(r->notes, VARY_KEY)) != NULL) {
-        apr_table_merge(r->headers_out, "Vary", vary);
-        apr_table_unset(r->notes, VARY_KEY);
+    /* expand the result */
+    if (!(p->flags & RULEFLAG_NOSUB)) {
+        newuri = do_expand(p->output, ctx);
+        rewritelog((r, 2, ctx->perdir, "rewrite '%s' -> '%s'", ctx->uri,
+                    newuri));
     }
 
-    /*
-     *  If this is a pure matching rule (`RewriteRule <pat> -')
-     *  we stop processing and return immediately. The only thing
-     *  we have not to forget are the environment variables and
-     *  cookies:
-     *  (`RewriteRule <pat> - [E=...,CO=...]')
-     */
-    if (output[0] == '-' && !output[1]) {
-        do_expand_env(r, p->env, briRR, briRC);
-        do_expand_cookie(r, p->cookie, briRR, briRC);
-        if (p->forced_mimetype != NULL) {
-            if (perdir == NULL) {
-                /* In the per-server context we can force the MIME-type
-                 * the correct way by notifying our MIME-type hook handler
-                 * to do the job when the MIME-type API stage is reached.
-                 */
-                rewritelog(r, 2, "remember %s to have MIME-type '%s'",
-                           r->filename, p->forced_mimetype);
-                apr_table_setn(r->notes, REWRITE_FORCED_MIMETYPE_NOTEVAR,
-                               p->forced_mimetype);
-            }
-            else {
-                /* In per-directory context we operate in the Fixup API hook
-                 * which is after the MIME-type hook, so our MIME-type handler
-                 * has no chance to set r->content_type. And because we are
-                 * in the situation where no substitution takes place no
-                 * sub-request will happen (which could solve the
-                 * restriction). As a workaround we do it ourself now
-                 * immediately although this is not strictly API-conforming.
-                 * But it's the only chance we have...
-                 */
-                rewritelog(r, 1, "[per-dir %s] force %s to have MIME-type "
-                           "'%s'", perdir, r->filename, p->forced_mimetype);
-                ap_set_content_type(r, p->forced_mimetype);
-            }
-        }
-        return 2;
-    }
+    /* expand [E=var:val] and [CO=<cookie>] */
+    do_expand_env(p->env, ctx);
+    do_expand_cookie(p->cookie, ctx);
 
-    /*
-     *  Ok, now we finally know all patterns have matched and
-     *  that there is something to replace, so we create the
-     *  substitution URL string in `newuri'.
-     */
-    newuri = do_expand(r, output, briRR, briRC);
-    if (perdir == NULL) {
-        rewritelog(r, 2, "rewrite %s -> %s", uri, newuri);
-    }
-    else {
-        rewritelog(r, 2, "[per-dir %s] rewrite %s -> %s", perdir, uri, newuri);
-    }
+    /* non-substitution rules ('RewriteRule <pat> -') end here. */
+    if (p->flags & RULEFLAG_NOSUB) {
+        force_type_handler(p, ctx);
 
-    /*
-     *  Additionally do expansion for the environment variable
-     *  strings (`RewriteRule .. .. [E=<string>]').
-     */
-    do_expand_env(r, p->env, briRR, briRC);
+        if (p->flags & RULEFLAG_STATUS) {
+            rewritelog((r, 2, ctx->perdir, "forcing responsecode %d for %s",
+                        p->forced_responsecode, r->filename));
 
-    /*
-     *  Also set cookies for any cookie strings
-     *  (`RewriteRule .. .. [CO=<string>]').
-     */
-    do_expand_cookie(r, p->cookie, briRR, briRC);
+            r->status = p->forced_responsecode;
+        }
 
-    /*
-     *  Now replace API's knowledge of the current URI:
-     *  Replace r->filename with the new URI string and split out
-     *  an on-the-fly generated QUERY_STRING part into r->args
-     */
-    r->filename = apr_pstrdup(r->pool, newuri);
+        return 2;
+    }
+
+    /* Now adjust API's knowledge about r->filename and r->args */
+    r->filename = newuri;
     splitout_queryargs(r, p->flags & RULEFLAG_QSAPPEND);
 
-    /*
-     *   Add the previously stripped per-directory location
-     *   prefix if the new URI is not a new one for this
-     *   location, i.e. if it's not an absolute URL (!) path nor
-     *   a fully qualified URL scheme.
+    /* Add the previously stripped per-directory location prefix, unless
+     * (1) it's an absolute URL path and
+     * (2) it's a full qualified URL
      */
-    if (prefixstrip && *r->filename != '/'
-                    && !is_absolute_uri(r->filename)) {
-        rewritelog(r, 3, "[per-dir %s] add per-dir prefix: %s -> %s%s",
-                   perdir, r->filename, perdir, r->filename);
-        r->filename = apr_pstrcat(r->pool, perdir, r->filename, NULL);
+    if (   ctx->perdir && !is_proxyreq && *r->filename != '/'
+        && !is_absolute_uri(r->filename)) {
+        rewritelog((r, 3, ctx->perdir, "add per-dir prefix: %s -> %s%s",
+                    r->filename, ctx->perdir, r->filename));
+
+        r->filename = apr_pstrcat(r->pool, ctx->perdir, r->filename, NULL);
     }
 
-    /*
-     *  If this rule is forced for proxy throughput
-     *  (`RewriteRule ... ... [P]') then emulate mod_proxy's
-     *  URL-to-filename handler to be sure mod_proxy is triggered
-     *  for this URL later in the Apache API. But make sure it is
-     *  a fully-qualified URL. (If not it is qualified with
-     *  ourself).
+    /* If this rule is forced for proxy throughput
+     * (`RewriteRule ... ... [P]') then emulate mod_proxy's
+     * URL-to-filename handler to be sure mod_proxy is triggered
+     * for this URL later in the Apache API. But make sure it is
+     * a fully-qualified URL. (If not it is qualified with
+     * ourself).
      */
     if (p->flags & RULEFLAG_PROXY) {
         fully_qualify_uri(r);
-        if (perdir == NULL) {
-            rewritelog(r, 2, "forcing proxy-throughput with %s", r->filename);
-        }
-        else {
-            rewritelog(r, 2, "[per-dir %s] forcing proxy-throughput with %s",
-                       perdir, r->filename);
-        }
+
+        rewritelog((r, 2, ctx->perdir, "forcing proxy-throughput with %s",
+                    r->filename));
+
         r->filename = apr_pstrcat(r->pool, "proxy:", r->filename, NULL);
         return 1;
     }
 
-    /*
-     *  If this rule is explicitly forced for HTTP redirection
-     *  (`RewriteRule .. .. [R]') then force an external HTTP
-     *  redirect. But make sure it is a fully-qualified URL. (If
-     *  not it is qualified with ourself).
+    /* If this rule is explicitly forced for HTTP redirection
+     * (`RewriteRule .. .. [R]') then force an external HTTP
+     * redirect. But make sure it is a fully-qualified URL. (If
+     * not it is qualified with ourself).
      */
     if (p->flags & RULEFLAG_FORCEREDIRECT) {
         fully_qualify_uri(r);
-        if (perdir == NULL) {
-            rewritelog(r, 2,
-                       "explicitly forcing redirect with %s", r->filename);
-        }
-        else {
-            rewritelog(r, 2,
-                       "[per-dir %s] explicitly forcing redirect with %s",
-                       perdir, r->filename);
-        }
+
+        rewritelog((r, 2, ctx->perdir, "explicitly forcing redirect with %s",
+                    r->filename));
+
         r->status = p->forced_responsecode;
         return 1;
     }
 
-    /*
-     *  Special Rewriting Feature: Self-Reduction
-     *  We reduce the URL by stripping a possible
-     *  http[s]://<ourhost>[:<port>] prefix, i.e. a prefix which
-     *  corresponds to ourself. This is to simplify rewrite maps
-     *  and to avoid recursion, etc. When this prefix is not a
-     *  coincidence then the user has to use [R] explicitly (see
-     *  above).
+    /* Special Rewriting Feature: Self-Reduction
+     * We reduce the URL by stripping a possible
+     * http[s]://<ourhost>[:<port>] prefix, i.e. a prefix which
+     * corresponds to ourself. This is to simplify rewrite maps
+     * and to avoid recursion, etc. When this prefix is not a
+     * coincidence then the user has to use [R] explicitly (see
+     * above).
      */
     reduce_uri(r);
 
-    /*
-     *  If this rule is still implicitly forced for HTTP
-     *  redirection (`RewriteRule .. <scheme>://...') then
-     *  directly force an external HTTP redirect.
+    /* If this rule is still implicitly forced for HTTP
+     * redirection (`RewriteRule .. <scheme>://...') then
+     * directly force an external HTTP redirect.
      */
     if (is_absolute_uri(r->filename)) {
-        if (perdir == NULL) {
-            rewritelog(r, 2,
-                       "implicitly forcing redirect (rc=%d) with %s",
-                       p->forced_responsecode, r->filename);
-        }
-        else {
-            rewritelog(r, 2, "[per-dir %s] implicitly forcing redirect "
-                       "(rc=%d) with %s", perdir, p->forced_responsecode,
-                       r->filename);
-        }
+        rewritelog((r, 2, ctx->perdir, "implicitly forcing redirect (rc=%d) "
+                    "with %s", p->forced_responsecode, r->filename));
+
         r->status = p->forced_responsecode;
         return 1;
     }
 
-    /*
-     *  Finally we had to remember if a MIME-type should be
-     *  forced for this URL (`RewriteRule .. .. [T=<type>]')
-     *  Later in the API processing phase this is forced by our
-     *  MIME API-hook function. This time it's no problem even for
-     *  the per-directory context (where the MIME-type hook was
-     *  already processed) because a sub-request happens ;-)
-     */
-    if (p->forced_mimetype != NULL) {
-        apr_table_setn(r->notes, REWRITE_FORCED_MIMETYPE_NOTEVAR,
-                      p->forced_mimetype);
-        if (perdir == NULL) {
-            rewritelog(r, 2, "remember %s to have MIME-type '%s'",
-                       r->filename, p->forced_mimetype);
-        }
-        else {
-            rewritelog(r, 2,
-                       "[per-dir %s] remember %s to have MIME-type '%s'",
-                       perdir, r->filename, p->forced_mimetype);
-        }
-    }
+    /* Finally remember the forced mime-type */
+    force_type_handler(p, ctx);
 
-    /*
-     *  Puuhhhhhhhh... WHAT COMPLICATED STUFF ;_)
-     *  But now we're done for this particular rule.
+    /* Puuhhhhhhhh... WHAT COMPLICATED STUFF ;_)
+     * But now we're done for this particular rule.
      */
     return 1;
 }
@@ -3734,6 +3874,11 @@ static int apply_rewrite_list(request_rec *r, apr_array_header_t *rewriterules,
     int changed;
     int rc;
     int s;
+    rewrite_ctx *ctx;
+
+    ctx = apr_palloc(r->pool, sizeof(*ctx));
+    ctx->perdir = perdir;
+    ctx->r = r;
 
     /*
      *  Iterate over all existing rules
@@ -3751,7 +3896,6 @@ static int apply_rewrite_list(request_rec *r, apr_array_header_t *rewriterules,
          */
         if (r->main != NULL &&
             (p->flags & RULEFLAG_IGNOREONSUBREQ ||
-             p->flags & RULEFLAG_PROXY          ||
              p->flags & RULEFLAG_FORCEREDIRECT    )) {
             continue;
         }
@@ -3759,10 +3903,27 @@ static int apply_rewrite_list(request_rec *r, apr_array_header_t *rewriterules,
         /*
          *  Apply the current rule.
          */
-        rc = apply_rewrite_rule(r, p, perdir);
+        ctx->vary = NULL;
+        rc = apply_rewrite_rule(p, ctx);
+
         if (rc) {
+            /* Regardless of what we do next, we've found a match. Check to see
+             * if any of the request header fields were involved, and add them
+             * to the Vary field of the response.
+             */
+            if (ctx->vary) {
+                apr_table_merge(r->headers_out, "Vary", ctx->vary);
+            }
+
+            /*
+             * The rule sets the response code (implies match-only)
+             */
+            if (p->flags & RULEFLAG_STATUS) {
+                return ACTION_STATUS;
+            }
+
             /*
-             *  Indicate a change if this was not a match-only rule.
+             * Indicate a change if this was not a match-only rule.
              */
             if (rc != 2) {
                 changed = ((p->flags & RULEFLAG_NOESCAPE)
@@ -3776,45 +3937,19 @@ static int apply_rewrite_list(request_rec *r, apr_array_header_t *rewriterules,
              *  modules like mod_alias, mod_userdir, etc.
              */
             if (p->flags & RULEFLAG_PASSTHROUGH) {
-                rewritelog(r, 2, "forcing '%s' to get passed through "
-                           "to next API URI-to-filename handler", r->filename);
+                rewritelog((r, 2, perdir, "forcing '%s' to get passed through "
+                           "to next API URI-to-filename handler", r->filename));
                 r->filename = apr_pstrcat(r->pool, "passthrough:",
                                          r->filename, NULL);
                 changed = ACTION_NORMAL;
                 break;
             }
 
-            /*
-             *  Rule has the "forbidden" flag set which means that
-             *  we stop processing and indicate this to the caller.
-             */
-            if (p->flags & RULEFLAG_FORBIDDEN) {
-                rewritelog(r, 2, "forcing '%s' to be forbidden", r->filename);
-                r->filename = apr_pstrcat(r->pool, "forbidden:",
-                                         r->filename, NULL);
-                changed = ACTION_NORMAL;
-                break;
-            }
-
-            /*
-             *  Rule has the "gone" flag set which means that
-             *  we stop processing and indicate this to the caller.
-             */
-            if (p->flags & RULEFLAG_GONE) {
-                rewritelog(r, 2, "forcing '%s' to be gone", r->filename);
-                r->filename = apr_pstrcat(r->pool, "gone:", r->filename, NULL);
-                changed = ACTION_NORMAL;
-                break;
-            }
-
             /*
              *  Stop processing also on proxy pass-through and
              *  last-rule and new-round flags.
              */
-            if (p->flags & RULEFLAG_PROXY) {
-                break;
-            }
-            if (p->flags & RULEFLAG_LASTRULE) {
+            if (p->flags & (RULEFLAG_PROXY | RULEFLAG_LASTRULE)) {
                 break;
             }
 
@@ -3901,6 +4036,7 @@ static int post_config(apr_pool_t *p,
     /* check if proxy module is available */
     proxy_available = (ap_find_linked_module("mod_proxy.c") != NULL);
 
+#ifndef REWRITELOG_DISABLED
     /* create the rewriting lockfiles in the parent */
     if ((rv = apr_global_mutex_create(&rewrite_log_lock, NULL,
                                       APR_LOCK_DEFAULT, p)) != APR_SUCCESS) {
@@ -3909,7 +4045,7 @@ static int post_config(apr_pool_t *p,
         return HTTP_INTERNAL_SERVER_ERROR;
     }
 
-#ifdef MOD_REWRITE_SET_MUTEX_PERMS
+#ifdef AP_NEED_SET_MUTEX_PERMS
     rv = unixd_set_global_mutex_perms(rewrite_log_lock);
     if (rv != APR_SUCCESS) {
         ap_log_error(APLOG_MARK, APLOG_CRIT, rv, s,
@@ -3917,7 +4053,8 @@ static int post_config(apr_pool_t *p,
                      "rewrite_log_lock; check User and Group directives");
         return HTTP_INTERNAL_SERVER_ERROR;
     }
-#endif
+#endif /* perms */
+#endif /* rewritelog */
 
     rv = rewritelock_create(s, p);
     if (rv != APR_SUCCESS) {
@@ -3932,9 +4069,11 @@ static int post_config(apr_pool_t *p,
      * - open the RewriteMap prg:xxx programs
      */
     for (; s; s = s->next) {
+#ifndef REWRITELOG_DISABLED
         if (!open_rewritelog(s, p)) {
             return HTTP_INTERNAL_SERVER_ERROR;
         }
+#endif
 
         if (!first_time) {
             if (run_rewritemap_programs(s, p) != APR_SUCCESS) {
@@ -3943,12 +4082,15 @@ static int post_config(apr_pool_t *p,
         }
     }
 
+    rewrite_ssl_lookup = APR_RETRIEVE_OPTIONAL_FN(ssl_var_lookup);
+    rewrite_is_https = APR_RETRIEVE_OPTIONAL_FN(ssl_is_https);
+
     return OK;
 }
 
 static void init_child(apr_pool_t *p, server_rec *s)
 {
-    apr_status_t rv;
+    apr_status_t rv = 0; /* get a rid of gcc warning (REWRITELOG_DISABLED) */
 
     if (lockname != NULL && *(lockname) != '\0') {
         rv = apr_global_mutex_child_init(&rewrite_mapr_lock_acquire,
@@ -3960,11 +4102,13 @@ static void init_child(apr_pool_t *p, server_rec *s)
         }
     }
 
+#ifndef REWRITELOG_DISABLED
     rv = apr_global_mutex_child_init(&rewrite_log_lock, NULL, p);
     if (rv != APR_SUCCESS) {
         ap_log_error(APLOG_MARK, APLOG_CRIT, rv, s,
                      "mod_rewrite: could not init rewrite log lock in child");
     }
+#endif
 
     /* create the lookup cache */
     if (!init_cache(p)) {
@@ -4057,7 +4201,7 @@ static int hook_uri2file(request_rec *r)
     thisurl = apr_table_get(r->subprocess_env, ENVVAR_SCRIPT_URL);
 
     /* set the variable */
-    var = apr_pstrcat(r->pool, ap_http_method(r), "://", thisserver, thisport,
+    var = apr_pstrcat(r->pool, ap_http_scheme(r), "://", thisserver, thisport,
                       thisurl, NULL);
     apr_table_setn(r->subprocess_env, ENVVAR_SCRIPT_URI, var);
 
@@ -4067,12 +4211,12 @@ static int hook_uri2file(request_rec *r)
          */
         if (r->filename == NULL) {
             r->filename = apr_pstrdup(r->pool, r->uri);
-            rewritelog(r, 2, "init rewrite engine with requested uri %s",
-                       r->filename);
+            rewritelog((r, 2, NULL, "init rewrite engine with requested uri %s",
+                        r->filename));
         }
         else {
-            rewritelog(r, 2, "init rewrite engine with passed filename %s."
-                       " Original uri = %s", r->filename, r->uri);
+            rewritelog((r, 2, NULL, "init rewrite engine with passed filename "
+                        "%s. Original uri = %s", r->filename, r->uri));
         }
 
         /*
@@ -4083,16 +4227,24 @@ static int hook_uri2file(request_rec *r)
                       apr_psprintf(r->pool,"%d",rulestatus));
     }
     else {
-        rewritelog(r, 2,
-                   "uri already rewritten. Status %s, Uri %s, r->filename %s",
-                   saved_rulestatus, r->uri, r->filename);
+        rewritelog((r, 2, NULL, "uri already rewritten. Status %s, Uri %s, "
+                    "r->filename %s", saved_rulestatus, r->uri, r->filename));
+
         rulestatus = atoi(saved_rulestatus);
     }
 
     if (rulestatus) {
         unsigned skip;
-        apr_size_t flen = strlen(r->filename);
+        apr_size_t flen;
+
+        if (ACTION_STATUS == rulestatus) {
+            int n = r->status;
+
+            r->status = HTTP_OK;
+            return n;
+        }
 
+        flen = r->filename ? strlen(r->filename) : 0;
         if (flen > 6 && strncmp(r->filename, "proxy:", 6) == 0) {
             /* it should be go on as an internal proxy request */
 
@@ -4121,11 +4273,13 @@ static int hook_uri2file(request_rec *r)
             }
 
             /* now make sure the request gets handled by the proxy handler */
-            r->proxyreq = PROXYREQ_REVERSE;
+            if (PROXYREQ_NONE == r->proxyreq) {
+                r->proxyreq = PROXYREQ_REVERSE;
+            }
             r->handler  = "proxy-server";
 
-            rewritelog(r, 1, "go-ahead with proxy request %s [OK]",
-                       r->filename);
+            rewritelog((r, 1, NULL, "go-ahead with proxy request %s [OK]",
+                        r->filename));
             return OK;
         }
         else if ((skip = is_absolute_uri(r->filename)) > 0) {
@@ -4134,7 +4288,8 @@ static int hook_uri2file(request_rec *r)
             /* it was finally rewritten to a remote URL */
 
             if (rulestatus != ACTION_NOESCAPE) {
-                rewritelog(r, 1, "escaping %s for redirect", r->filename);
+                rewritelog((r, 1, NULL, "escaping %s for redirect",
+                            r->filename));
                 r->filename = escape_absolute_uri(r->pool, r->filename, skip);
             }
 
@@ -4158,17 +4313,11 @@ static int hook_uri2file(request_rec *r)
 
             /* now do the redirection */
             apr_table_setn(r->headers_out, "Location", r->filename);
-            rewritelog(r, 1, "redirect to %s [REDIRECT/%d]", r->filename, n);
+            rewritelog((r, 1, NULL, "redirect to %s [REDIRECT/%d]", r->filename,
+                        n));
+
             return n;
         }
-        else if (flen > 10 && strncmp(r->filename, "forbidden:", 10) == 0) {
-            /* This URLs is forced to be forbidden for the requester */
-            return HTTP_FORBIDDEN;
-        }
-        else if (flen > 5 && strncmp(r->filename, "gone:", 5) == 0) {
-            /* This URLs is forced to be gone */
-            return HTTP_GONE;
-        }
         else if (flen > 12 && strncmp(r->filename, "passthrough:", 12) == 0) {
             /*
              * Hack because of underpowered API: passing the current
@@ -4188,7 +4337,7 @@ static int hook_uri2file(request_rec *r)
 #if APR_HAS_USER
             r->filename = expand_tildepaths(r, r->filename);
 #endif
-            rewritelog(r, 2, "local path result: %s", r->filename);
+            rewritelog((r, 2, NULL, "local path result: %s", r->filename));
 
             /* the filename must be either an absolute local path or an
              * absolute local URL.
@@ -4227,22 +4376,22 @@ static int hook_uri2file(request_rec *r)
                 r->uri = tmp;
 
                 if (res != OK) {
-                    rewritelog(r, 1, "prefixing with document_root of %s "
-                                     "FAILED", r->filename);
+                    rewritelog((r, 1, NULL, "prefixing with document_root of %s"
+                                " FAILED", r->filename));
 
                     return res;
                 }
 
-                rewritelog(r, 2, "prefixed with document_root to %s",
-                           r->filename);
+                rewritelog((r, 2, NULL, "prefixed with document_root to %s",
+                            r->filename));
             }
 
-            rewritelog(r, 1, "go-ahead with %s [OK]", r->filename);
+            rewritelog((r, 1, NULL, "go-ahead with %s [OK]", r->filename));
             return OK;
         }
     }
     else {
-        rewritelog(r, 1, "pass through %s", r->filename);
+        rewritelog((r, 1, NULL, "pass through %s", r->filename));
         return DECLINED;
     }
 }
@@ -4261,6 +4410,7 @@ static int hook_fixup(request_rec *r)
     int rulestatus;
     int n;
     char *ofilename;
+    int is_proxyreq;
 
     dconf = (rewrite_perdir_conf *)ap_get_module_config(r->per_dir_config,
                                                         &rewrite_module);
@@ -4270,27 +4420,30 @@ static int hook_fixup(request_rec *r)
         return DECLINED;
     }
 
-    /* we shouldn't do anything in subrequests */
-    if (r->main != NULL) {
-        return DECLINED;
-    }
-
     /* if there are no real (i.e. no RewriteRule directives!)
        per-dir config of us, we return also immediately */
     if (dconf->directory == NULL) {
         return DECLINED;
     }
 
+    /*
+     * Proxy request?
+     */
+    is_proxyreq = (   r->proxyreq && r->filename
+                   && !strncmp(r->filename, "proxy:", 6));
+
     /*
      *  .htaccess file is called before really entering the directory, i.e.:
      *  URL: http://localhost/foo  and .htaccess is located in foo directory
      *  Ignore such attempts, since they may lead to undefined behaviour.
      */
-    l = strlen(dconf->directory) - 1;
-    if (r->filename && strlen(r->filename) == l &&
-        (dconf->directory)[l] == '/' &&
-        !strncmp(r->filename, dconf->directory, l)) {
-        return DECLINED;
+    if (!is_proxyreq) {
+        l = strlen(dconf->directory) - 1;
+        if (r->filename && strlen(r->filename) == l &&
+            (dconf->directory)[l] == '/' &&
+            !strncmp(r->filename, dconf->directory, l)) {
+            return DECLINED;
+        }
     }
 
     /*
@@ -4318,17 +4471,32 @@ static int hook_fixup(request_rec *r)
      *  remember the current filename before rewriting for later check
      *  to prevent deadlooping because of internal redirects
      *  on final URL/filename which can be equal to the inital one.
+     *  also, we'll restore original r->filename if we decline this
+     *  request
      */
     ofilename = r->filename;
 
+    if (r->filename == NULL) {
+        r->filename = apr_pstrdup(r->pool, r->uri);
+        rewritelog((r, 2, "init rewrite engine with requested uri %s",
+                    r->filename));
+    }
+
     /*
      *  now apply the rules ...
      */
     rulestatus = apply_rewrite_list(r, dconf->rewriterules, dconf->directory);
     if (rulestatus) {
         unsigned skip;
-        l = strlen(r->filename);
 
+        if (ACTION_STATUS == rulestatus) {
+            int n = r->status;
+
+            r->status = HTTP_OK;
+            return n;
+        }
+
+        l = strlen(r->filename);
         if (l > 6 && strncmp(r->filename, "proxy:", 6) == 0) {
             /* it should go on as an internal proxy request */
 
@@ -4343,11 +4511,13 @@ static int hook_fixup(request_rec *r)
             }
 
             /* now make sure the request gets handled by the proxy handler */
-            r->proxyreq = PROXYREQ_REVERSE;
+            if (PROXYREQ_NONE == r->proxyreq) {
+                r->proxyreq = PROXYREQ_REVERSE;
+            }
             r->handler  = "proxy-server";
 
-            rewritelog(r, 1, "[per-dir %s] go-ahead with proxy request "
-                       "%s [OK]", dconf->directory, r->filename);
+            rewritelog((r, 1, dconf->directory, "go-ahead with proxy request "
+                        "%s [OK]", r->filename));
             return OK;
         }
         else if ((skip = is_absolute_uri(r->filename)) > 0) {
@@ -4362,11 +4532,9 @@ static int hook_fixup(request_rec *r)
                 cp = r->filename + skip;
 
                 if ((cp = ap_strchr(cp, '/')) != NULL && *(++cp)) {
-                    rewritelog(r, 2,
-                               "[per-dir %s] trying to replace "
-                               "prefix %s with %s",
-                               dconf->directory, dconf->directory,
-                               dconf->baseurl);
+                    rewritelog((r, 2, dconf->directory, 
+                                "trying to replace prefix %s with %s",
+                                dconf->directory, dconf->baseurl));
 
                     /* I think, that hack needs an explanation:
                      * well, here is it:
@@ -4404,8 +4572,8 @@ static int hook_fixup(request_rec *r)
 
             /* now prepare the redirect... */
             if (rulestatus != ACTION_NOESCAPE) {
-                rewritelog(r, 1, "[per-dir %s] escaping %s for redirect",
-                           dconf->directory, r->filename);
+                rewritelog((r, 1, dconf->directory, "escaping %s for redirect",
+                            r->filename));
                 r->filename = escape_absolute_uri(r->pool, r->filename, skip);
             }
 
@@ -4429,18 +4597,10 @@ static int hook_fixup(request_rec *r)
 
             /* now do the redirection */
             apr_table_setn(r->headers_out, "Location", r->filename);
-            rewritelog(r, 1, "[per-dir %s] redirect to %s [REDIRECT/%d]",
-                       dconf->directory, r->filename, n);
+            rewritelog((r, 1, dconf->directory, "redirect to %s [REDIRECT/%d]",
+                        r->filename, n));
             return n;
         }
-        else if (l > 10 && strncmp(r->filename, "forbidden:", 10) == 0) {
-            /* This URL is forced to be forbidden for the requester */
-            return HTTP_FORBIDDEN;
-        }
-        else if (l > 5 && strncmp(r->filename, "gone:", 5) == 0) {
-            /* This URL is forced to be gone */
-            return HTTP_GONE;
-        }
         else {
             /* it was finally rewritten to a local path */
 
@@ -4467,10 +4627,9 @@ static int hook_fixup(request_rec *r)
              * use the following internal redirection stuff because
              * this would lead to a deadloop.
              */
-            if (strcmp(r->filename, ofilename) == 0) {
-                rewritelog(r, 1, "[per-dir %s] initial URL equal rewritten "
-                           "URL: %s [IGNORING REWRITE]",
-                           dconf->directory, r->filename);
+            if (ofilename != NULL && strcmp(r->filename, ofilename) == 0) {
+                rewritelog((r, 1, dconf->directory, "initial URL equal rewritten"
+                            " URL: %s [IGNORING REWRITE]", r->filename));
                 return OK;
             }
 
@@ -4481,9 +4640,9 @@ static int hook_fixup(request_rec *r)
              * plain URL
              */
             if (dconf->baseurl != NULL) {
-                rewritelog(r, 2,
-                           "[per-dir %s] trying to replace prefix %s with %s",
-                           dconf->directory, dconf->directory, dconf->baseurl);
+                rewritelog((r, 2, dconf->directory, "trying to replace prefix "
+                            "%s with %s", dconf->directory, dconf->baseurl));
+
                 r->filename = subst_prefix_path(r, r->filename,
                                                 dconf->directory,
                                                 dconf->baseurl);
@@ -4502,104 +4661,60 @@ static int hook_fixup(request_rec *r)
                     }
                     if (!strncmp(r->filename, ccp, l) &&
                         r->filename[l] == '/') {
-                        rewritelog(r, 2,
-                                   "[per-dir %s] strip document_root "
-                                   "prefix: %s -> %s",
-                                   dconf->directory, r->filename,
-                                   r->filename+l);
+                        rewritelog((r, 2,dconf->directory, "strip document_root"
+                                    " prefix: %s -> %s", r->filename,
+                                    r->filename+l));
+
                         r->filename = apr_pstrdup(r->pool, r->filename+l);
                     }
                 }
             }
 
             /* now initiate the internal redirect */
-            rewritelog(r, 1, "[per-dir %s] internal redirect with %s "
-                       "[INTERNAL REDIRECT]", dconf->directory, r->filename);
+            rewritelog((r, 1, dconf->directory, "internal redirect with %s "
+                        "[INTERNAL REDIRECT]", r->filename));
             r->filename = apr_pstrcat(r->pool, "redirect:", r->filename, NULL);
             r->handler = "redirect-handler";
             return OK;
         }
     }
     else {
-        rewritelog(r, 1, "[per-dir %s] pass through %s",
-                   dconf->directory, r->filename);
+        rewritelog((r, 1, dconf->directory, "pass through %s", r->filename));
+        r->filename = ofilename;
         return DECLINED;
     }
 }
 
 /*
  * MIME-type hook
- * [T=...] in server-context
+ * [T=...,H=...] execution
  */
 static int hook_mimetype(request_rec *r)
 {
     const char *t;
 
-    /* now check if we have to force a MIME-type */
+    /* type */
     t = apr_table_get(r->notes, REWRITE_FORCED_MIMETYPE_NOTEVAR);
-    if (t == NULL) {
-        return DECLINED;
-    }
-    else {
-        rewritelog(r, 1, "force filename %s to have MIME-type '%s'",
-                   r->filename, t);
-        ap_set_content_type(r, t);
-        return OK;
-    }
-}
-
-/* check whether redirect limit is reached */
-static int is_redirect_limit_exceeded(request_rec *r)
-{
-    request_rec *top = r;
-    rewrite_request_conf *reqc;
-    rewrite_perdir_conf *dconf;
-
-    /* we store it in the top request */
-    while (top->main) {
-        top = top->main;
-    }
-    while (top->prev) {
-        top = top->prev;
-    }
-
-    /* fetch our config */
-    reqc = (rewrite_request_conf *) ap_get_module_config(top->request_config,
-                                                         &rewrite_module);
+    if (t && *t) {
+        rewritelog((r, 1, NULL, "force filename %s to have MIME-type '%s'",
+                    r->filename, t));
 
-    /* no config there? create one. */
-    if (!reqc) {
-        rewrite_server_conf *sconf;
-
-        reqc = apr_palloc(top->pool, sizeof(rewrite_request_conf));
-        sconf = ap_get_module_config(r->server->module_config, &rewrite_module);
-
-        reqc->redirects = 0;
-        reqc->redirect_limit = sconf->redirect_limit
-                                 ? sconf->redirect_limit
-                                 : REWRITE_REDIRECT_LIMIT;
-
-        /* associate it with this request */
-        ap_set_module_config(top->request_config, &rewrite_module, reqc);
+        ap_set_content_type(r, t);
     }
 
-    /* allow to change the limit during redirects. */
-    dconf = (rewrite_perdir_conf *)ap_get_module_config(r->per_dir_config,
-                                                        &rewrite_module);
+    /* handler */
+    t = apr_table_get(r->notes, REWRITE_FORCED_HANDLER_NOTEVAR);
+    if (t && *t) {
+        rewritelog((r, 1, NULL, "force filename %s to have the "
+                    "Content-handler '%s'", r->filename, t));
 
-    /* 0 == unset; take server conf ... */
-    if (dconf->redirect_limit) {
-        reqc->redirect_limit = dconf->redirect_limit;
+        r->handler = t;
     }
 
-    ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
-                  "mod_rewrite's internal redirect status: %d/%d.",
-                  reqc->redirects, reqc->redirect_limit);
-
-    /* and now give the caller a hint */
-    return (reqc->redirects++ >= reqc->redirect_limit);
+    return OK;
 }
 
+
 /*
  * "content" handler for internal redirects
  */
@@ -4614,15 +4729,6 @@ static int handler_redirect(request_rec *r)
         return DECLINED;
     }
 
-    if (is_redirect_limit_exceeded(r)) {
-        ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
-                      "mod_rewrite: maximum number of internal redirects "
-                      "reached. Assuming configuration error. Use "
-                      "'RewriteOptions MaxRedirects' to increase the limit "
-                      "if neccessary.");
-        return HTTP_INTERNAL_SERVER_ERROR;
-    }
-
     /* now do the internal redirect */
     ap_internal_redirect(apr_pstrcat(r->pool, r->filename+9,
                                      r->args ? "?" : NULL, r->args, NULL), r);
@@ -4640,6 +4746,16 @@ static int handler_redirect(request_rec *r)
  * +-------------------------------------------------------+
  */
 
+#ifdef REWRITELOG_DISABLED
+static const char *fake_rewritelog(cmd_parms *cmd, void *dummy, const char *a1)
+{
+    return "RewriteLog and RewriteLogLevel are not supported by this build "
+           "of mod_rewrite because it was compiled using the "
+           "-DREWRITELOG_DISABLED compiler option. You have to recompile "
+           "mod_rewrite WITHOUT this option in order to use the rewrite log.";
+}
+#endif
+
 static const command_rec command_table[] = {
     AP_INIT_FLAG(    "RewriteEngine",   cmd_rewriteengine,  NULL, OR_FILEINFO,
                      "On or Off to enable or disable (default) the whole "
@@ -4657,11 +4773,18 @@ static const command_rec command_table[] = {
     AP_INIT_TAKE1(   "RewriteLock",     cmd_rewritelock,     NULL, RSRC_CONF,
                      "the filename of a lockfile used for inter-process "
                      "synchronization"),
+#ifndef REWRITELOG_DISABLED
     AP_INIT_TAKE1(   "RewriteLog",      cmd_rewritelog,      NULL, RSRC_CONF,
                      "the filename of the rewriting logfile"),
     AP_INIT_TAKE1(   "RewriteLogLevel", cmd_rewriteloglevel, NULL, RSRC_CONF,
                      "the level of the rewriting logfile verbosity "
                      "(0=none, 1=std, .., 9=max)"),
+#else
+    AP_INIT_TAKE1(   "RewriteLog", fake_rewritelog, NULL, RSRC_CONF,
+                     "[DISABLED] the filename of the rewriting logfile"),
+    AP_INIT_TAKE1(   "RewriteLogLevel", fake_rewritelog, NULL, RSRC_CONF,
+                     "[DISABLED] the level of the rewriting logfile verbosity"),
+#endif
     { NULL }
 };
 
@@ -4677,11 +4800,6 @@ static void register_hooks(apr_pool_t *p)
      */
     static const char * const aszPre[]={ "mod_proxy.c", NULL };
 
-    /* check type before mod_mime, so that [T=foo/bar] will not be
-     * overridden by AddType definitions.
-     */
-    static const char * const ct_aszSucc[]={ "mod_mime.c", NULL };
-
     APR_REGISTER_OPTIONAL_FN(ap_register_rewrite_mapfunc);
 
     ap_hook_handler(handler_redirect, NULL, NULL, APR_HOOK_MIDDLE);
@@ -4690,8 +4808,8 @@ static void register_hooks(apr_pool_t *p)
     ap_hook_child_init(init_child, NULL, NULL, APR_HOOK_MIDDLE);
 
     ap_hook_fixups(hook_fixup, aszPre, NULL, APR_HOOK_FIRST);
+    ap_hook_fixups(hook_mimetype, NULL, NULL, APR_HOOK_LAST);
     ap_hook_translate_name(hook_uri2file, NULL, NULL, APR_HOOK_FIRST);
-    ap_hook_type_checker(hook_mimetype, NULL, ct_aszSucc, APR_HOOK_MIDDLE);
 }
 
     /* the main config structure */