--- modules/mappers/mod_rewrite.c.old	2005-02-04 21:21:18.000000000 +0100
+++ modules/mappers/mod_rewrite.c	2005-05-06 09:58:31.000000000 +0200
@@ -487,7 +487,14 @@
                                a2+4, NULL);
         }
     }
-    else {
+#ifndef NO_MYSQL_REWRITEMAP
+    else if (strncmp(a2, "mysql:", 6) == 0) {
+        newmap->type      = MAPTYPE_MYSQL;
+        newmap->datafile  = a2+6;
+        newmap->checkfile = a2+6;
+    }
+#endif
+          else {
         newmap->type      = MAPTYPE_TXT;
         newmap->datafile  = a2;
         newmap->checkfile = a2;
@@ -2999,6 +3006,13 @@
     apr_finfo_t st;
     apr_status_t rv;
     int i;
+#ifndef NO_MYSQL_REWRITEMAP
+    MYSQL mysql_db;
+    MYSQL_RES *mysql_result;
+    MYSQL_ROW mysql_row;
+    char *mysqlserver, *mysqluser, *mysqlpasswd, *mysqlbase, *mysqlport;
+    char *mysqlrequest, *tmp, *tmp2;
+#endif
 
     /* get map configuration */
     conf = ap_get_module_config(r->server->module_config, &rewrite_module);
@@ -3024,7 +3038,7 @@
                     rewritelog(r, 6, "cache lookup FAILED, forcing new "
                                "map lookup");
                     if ((value =
-                         lookup_map_txtfile(r, s->datafile, key)) != NULL) {
+                         lookup_map_txtfile(r, s->datafile, key, 0)) != NULL) {
                         rewritelog(r, 5, "map lookup OK: map=%s key=%s[txt] "
                                    "-> val=%s", s->name, key, value);
                         set_cache_string(cachep, s->cachename, CACHEMODE_TS,
@@ -3121,7 +3135,7 @@
                     rewritelog(r, 6, "cache lookup FAILED, forcing new "
                                "map lookup");
                     if ((value =
-                         lookup_map_txtfile(r, s->datafile, key)) != NULL) {
+                         lookup_map_txtfile(r, s->datafile, key, 0)) != NULL) {
                         rewritelog(r, 5, "map lookup OK: map=%s key=%s[txt] "
                                    "-> val=%s", s->name, key, value);
                         set_cache_string(cachep, s->cachename, CACHEMODE_TS,
@@ -3149,12 +3163,171 @@
                 }
                 return value;
             }
+#ifndef NO_MYSQL_REWRITEMAP
+            else if (s->type == MAPTYPE_MYSQL) {
+              /* get values from configfile */
+              if ((rv = apr_stat(&st, s->checkfile,
+                                APR_FINFO_MIN, r->pool)) != APR_SUCCESS) {
+                ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
+                              "mod_rewrite: can't access mysql RewriteMap "
+                                  "file %s", s->checkfile);
+                rewritelog(r, 1, "can't open RewriteMap file, "
+                    "see error log");
+                return NULL;
+              }
+  /* define a local macro to make the code smaller */
+#define GET_CACHE_VALUE(VAR,TXT) {                                        \
+    VAR = get_cache_string(cachep, s->name, CACHEMODE_TS,                 \
+                           st.mtime,TXT);                                 \
+    if (VAR == NULL) {                                                    \
+        rewritelog(r, 6, "cache lookup FAILED, forcing new "              \
+                   "map lookup");                                         \
+        if ((VAR = lookup_map_txtfile(r, s->datafile, TXT, 1)) != NULL) { \
+            rewritelog(r, 5, "map lookup OK: map=%s key=%s[txt] "         \
+                       "-> val=%s", s->name, TXT, VAR);                   \
+            set_cache_string(cachep, s->name, CACHEMODE_TS,               \
+                             st.mtime, TXT, VAR);                         \
+            }                                                             \
+        else {                                                            \
+            rewritelog(r, 5, "map lookup FAILED: map=%s[txt] "            \
+                       "key=%s", s->name, TXT);                           \
+            if (!strncmp(TXT,"port",5)) {                                 \
+                set_cache_string(cachep, s->name, CACHEMODE_TS,           \
+                                 st.mtime, TXT, "3306");                  \
+                VAR = get_cache_string(cachep, s->name, CACHEMODE_TS,     \
+                                       st.mtime,TXT);                     \
+            }                                                             \
+            else {                                                        \
+                set_cache_string(cachep, s->name, CACHEMODE_TS,           \
+                                 st.mtime, TXT, "");                      \
+                return NULL;                                              \
+            }                                                             \
+            }                                                             \
+            }                                                             \
+    else {                                                                \
+        rewritelog(r, 5, "cache lookup OK: map=%s[txt] key=%s "           \
+                   "-> val=%s", s->name, TXT, VAR);                       \
+        if (VAR[0] == '\0') {                                             \
+            return NULL;                                                  \
+            }                                                             \
+            }                                                             \
+            }
+
+                /* read the database infos from file : */
+                GET_CACHE_VALUE(mysqlserver, "server");  /* servername  */
+                GET_CACHE_VALUE(mysqlport, "port");      /* server port */
+                GET_CACHE_VALUE(mysqluser, "user");      /* user login  */
+                GET_CACHE_VALUE(mysqlpasswd, "passwd");  /* password    */
+                GET_CACHE_VALUE(mysqlbase, "base");      /* database    */
+                /* select something from table where somearg='$1' */
+                /* $1 will be changed with key value              */
+                /* only first result will be returned             */
+                GET_CACHE_VALUE(mysqlrequest, "select");
+#undef GET_CACHE_VALUE
+                /* now replace each '$1' by the key string */
+                /* if key is '$1', we have a problem */
+                if (strstr(key,"$1")) {
+                  return NULL;
+                }
+                tmp = mysqlrequest;
+                while (tmp[0]) {
+                  if ((tmp[0] == '$') && (tmp[1] == '1')) { /* found */
+                    /* first enlarge the string */
+                    mysqlrequest = apr_pstrcat(r->pool, mysqlrequest, key,
+                                              NULL);
+                    /* cut the string at the old size (we still have memory */
+                    /* after the final '\0' */
+                    mysqlrequest[strlen(mysqlrequest) - strlen(key)] = 0;
+                    /* restart from begining as the pointer may have change */
+                    tmp = mysqlrequest;
+                    while ((tmp[0] != '$') && (tmp[1] != '1')) {
+                      tmp++;
+                    }
+                    if (strlen(key) == 1) {
+                      /* put the only character of the key string and */
+                      /* scroll the rest of the text from one character */
+                      tmp[0] = key[0];
+                      tmp++;
+                      while (tmp[0]) {
+                        tmp[0] = tmp[1];
+                        tmp++;
+                      }
+                    }
+                    else {
+                      tmp[0] = key[0];
+                      tmp[1] = key[1];
+                      tmp += 2;
+                      /* put the first 2 characters from key */
+                      if (strlen(key) > 2) {
+                        /* insert the rest of the key string : */
+                        /* first scroll the text to the right  */
+                        tmp2 = mysqlrequest + strlen(mysqlrequest)
+                            + strlen(key);
+                        i = strlen(key) - 2;
+                        while (tmp2 != tmp) {
+                          tmp2[0] = (tmp2 - i)[0];
+                          tmp2--;
+                        }
+                        /* now insert the remaining chars */
+                        tmp2 = key + 2;
+                        while (tmp2[0]) {
+                          tmp[0] = tmp2[0];
+                          tmp++;
+                          tmp2++;
+                        }
+                      }
+                    }
+                  }
+                  tmp++;
+                }
+                /*now let see if we already now the query answer (from cache)*/
+                /*or get it from mysql                                       */
+                value = get_cache_string(cachep, s->name, CACHEMODE_TS,
+                                         st.mtime, mysqlrequest);
+                if (value == NULL) {
+                  rewritelog(r, 6, "cache lookup FAILED, forcing new "
+                      "map lookup");
+                  mysql_init(&mysql_db);
+                  if (mysql_real_connect(&mysql_db, mysqlserver, mysqluser,
+                      mysqlpasswd, mysqlbase, 
+                      atoi(mysqlport), "", 0)) {
+                        if (!mysql_query(&mysql_db, mysqlrequest)) {
+                          mysql_result = mysql_store_result(&mysql_db);
+                          mysql_row = mysql_fetch_row(mysql_result);
+                          mysql_close(&mysql_db);
+                          value = apr_pstrdup(r->pool, mysql_row[0]);
+                          rewritelog(r, 5, "map lookup OK: map=%s key=%s[txt] ",
+                                     st.mtime, mysqlrequest, value);
+                          set_cache_string(cachep, s->name, CACHEMODE_TS,
+                                           st.mtime, mysqlrequest, value);
+                          return value;
+                        }
+                        else {
+                          rewritelog(r, 5, "map lookup FAILED: map=%s[txt] "
+                              "-> val=%s", s->name, mysqlrequest, value);
+                          set_cache_string(cachep, s->name, CACHEMODE_TS,
+                                           st.mtime, mysqlrequest, "");
+                        }
+                        mysql_close(&mysql_db);
+                      }
+                      return NULL;
+                }
+                else {
+                  rewritelog(r, 5, "cache lookup OK: map=%s[txt] key=%s "
+                      "-> val=%s", s->name, mysqlrequest, value);
+                  if (value[0] == '\0') {
+                    return NULL;
+                  }
+                  return value;
+                }
+            }
+#endif
         }
     }
     return NULL;
 }
 
-static char *lookup_map_txtfile(request_rec *r, const char *file, char *key)
+static char *lookup_map_txtfile(request_rec *r, const char *file, char *key, int accept_several_words)
 {
     apr_file_t *fp = NULL;
     apr_status_t rc;
@@ -3191,7 +3364,14 @@
         skip = strspn(cpT, " \t\r\n");
         cpT += skip;
         curval = cpT;
-        skip = strcspn(cpT, " \t\r\n");
+        if (accept_several_words)
+        {
+          skip = strcspn(cpT, "\t\r\n");
+        }
+        else
+        {
+          skip = strcspn(cpT, " \t\r\n");
+        }
         if (skip == 0) {
             continue; /* no value... */
         }
@@ -3768,7 +3948,7 @@
 
 static char *lookup_variable(request_rec *r, char *var)
 {
-    const char *result;
+    const char *result, *tmp;
     char resultbuf[LONG_STRING_LEN];
     apr_time_exp_t tm;
     request_rec *rsub;
@@ -3857,6 +4037,19 @@
     else if (strcasecmp(var, "SERVER_NAME") == 0) {
         result = ap_get_server_name(r);
     }
+    else if (strcasecmp(var, "SUBDOMAIN_NAME") == 0) {
+      /* first get the full server name */
+      result = apr_pstrdup(r->pool, ap_get_server_name(r));
+      /* now remove the domain name */
+      tmp=strrchr(result,'.');
+      if (tmp) {
+        *tmp=0;
+        tmp=strrchr(result,'.');
+        if (tmp) {
+          *tmp=0;
+        }
+      }
+    }
     else if (strcasecmp(var, "SERVER_ADDR") == 0) { /* non-standard */
         result = r->connection->local_ip;
     }
--- modules/mappers/mod_rewrite.h.old	2005-02-04 21:21:18.000000000 +0100
+++ modules/mappers/mod_rewrite.h	2005-05-06 07:33:45.000000000 +0200
@@ -77,6 +77,10 @@
 #include "apr_dbm.h"
 #include "ap_config.h"
 
+#ifndef NO_MYSQL_REWRITEMAP
+#include <mysql/mysql.h>
+#endif
+
     /* Include from the Apache server ... */
 #define CORE_PRIVATE
 #include "httpd.h"
@@ -132,6 +136,9 @@
 #define MAPTYPE_PRG                 1<<2
 #define MAPTYPE_INT                 1<<3
 #define MAPTYPE_RND                 1<<4
+#ifndef NO_MYSQL_REWRITEMAP
+#define MAPTYPE_MYSQL               1<<5
+#endif
 
 #define ENGINE_DISABLED             1<<0
 #define ENGINE_ENABLED              1<<1
@@ -376,7 +383,7 @@
 
     /* rewrite map support functions */
 static char *lookup_map(request_rec *r, char *name, char *key);
-static char *lookup_map_txtfile(request_rec *r, const char *file, char *key);
+static char *lookup_map_txtfile(request_rec *r, const char *file, char *key, int accept_several_words);
 static char *lookup_map_dbmfile(request_rec *r, const char *file, 
                                 const char *dbmtype, char *key);
 static char *lookup_map_program(request_rec *r, apr_file_t *fpin,
