diff -Naur mod_vhost_cdb-1.0/mod_vhost_cdb.c mod_vhost_cdb-1.1/mod_vhost_cdb.c
--- mod_vhost_cdb-1.0/mod_vhost_cdb.c	2008-04-04 15:41:24.000000000 +0200
+++ mod_vhost_cdb-1.1/mod_vhost_cdb.c	2008-04-13 19:32:30.000000000 +0200
@@ -46,8 +46,8 @@
 static char 		*get_config_from_db(request_rec *, struct cdb *, const char *, mod_vhost_cdb_config *);
 static apr_table_t 	*create_config_table(request_rec *, const char *);
 static int 		mod_vhost_cdb_set_document_root(request_rec *, apr_table_t *);
-int 			mod_vhost_cdb_set_script_alias(request_rec *, apr_table_t *);
-int 			mod_vhost_cdb_export_vars(request_rec *, apr_table_t *);
+static int		mod_vhost_cdb_set_script_alias(request_rec *, apr_table_t *);
+static int		mod_vhost_cdb_export_vars(request_rec *, apr_table_t *);
 static int 		expand_path(request_rec *, apr_table_t *, const char *);
 
 static int 		mod_vhost_cdb_trans_uri(request_rec *);
@@ -151,32 +151,54 @@
 
 static int mod_vhost_cdb_set_document_root(request_rec *r, apr_table_t *conf)
 {
-if(r->hostname!=NULL)
+/*
+ * if there is a ServerName defined, use this one 
+ * instead of r->hostname
+ */
+
+char *hn=(char *) r->hostname;
+
+if(apr_table_get(conf,"ServerName"))
+	{
+	hn=apr_pstrdup(r->pool,apr_table_get(conf,"ServerName"));
+	}
+if(hn!=NULL)
 	{
-	r->server->server_hostname = apr_pstrdup(r->server->process->pool, r->hostname);
-	r->parsed_uri.hostinfo = apr_pstrdup(r->pool,r->hostname);
-	r->parsed_uri.hostname = apr_pstrdup(r->pool,r->hostname);
+	/* setting r->server->server_hostname is dangerous, but WTF, if there is no other way... */ 
+	/* this is NOT THREADSAFE! */
+	
+	ap_log_rerror(APLOG_MARK,APLOG_DEBUG,0,r,"set_doc_root: server_hostname: %s",hn); 
+	r->server->server_hostname = apr_pstrdup(r->server->process->pool, hn); 
+	r->parsed_uri.hostinfo = apr_pstrdup(r->pool,hn);
+	r->parsed_uri.hostname = apr_pstrdup(r->pool,hn);
 	}
 
 r->server->is_virtual = 1;
 
 /* ap_log_rerror(APLOG_MARK,APLOG_DEBUG,0,r,"set_doc_root: server_hostname: %s",r->server->server_hostname); */
 
+/*
+ * set_script_alias returns 0 if this is not a script-aliased request
+ */
+
+char *document_root=(char *) apr_table_get(conf,"DocumentRoot");
+
 if(!mod_vhost_cdb_set_script_alias(r,conf))
 	{
 	if(r->uri[0]!='/')
 		{
 		return 0;
 		}
-	r->filename=apr_pstrcat(r->pool,apr_table_get(conf,"DocumentRoot"),r->uri,NULL);
+	r->filename=apr_pstrcat(r->pool,document_root,r->uri,NULL);
 	ap_no2slash(r->filename);
+	/* ap_log_rerror(APLOG_MARK,APLOG_DEBUG,0,r,"set_doc_root: r->filename: %s",r->filename); */
 	}
 
-char filter[1024];
-snprintf(filter,1024,"%s",apr_table_get(conf,"DocumentRoot"));
+char *filter=apr_pstrndup(r->pool, document_root, 1024);
 ap_no2slash(filter);
-apr_table_setn(r->subprocess_env, "SERVER_ROOT", apr_pstrdup(r->pool,filter));
-apr_table_setn(r->subprocess_env, "DOCUMENT_ROOT", apr_pstrdup(r->pool,filter));
+
+apr_table_setn(r->subprocess_env, "SERVER_ROOT", filter);
+apr_table_setn(r->subprocess_env, "DOCUMENT_ROOT", filter);
 
 /*
  * put the document_root into the core configuration
@@ -184,12 +206,11 @@
  */
 
 core_server_config *core=(core_server_config *) ap_get_module_config(r->server->module_config, &core_module);
-core->ap_document_root = apr_pstrdup(r->server->process->pool, apr_table_get(conf,"DocumentRoot"));
+core->ap_document_root = apr_pstrdup(r->server->process->pool, document_root);
 
 return 1;
 }
 
-
 /* ****************************************************************************	*/
 /* mod_vhost_cdb_set_script_alias						*/
 /* sets all needed variables to apply the ScriptAlias from cdb			*/
@@ -198,18 +219,18 @@
 /* code is actually borrowed from mod_vhost_ldap.c				*/
 /* ****************************************************************************	*/
 
-int mod_vhost_cdb_set_script_alias(request_rec *r, apr_table_t *conf)
+static int mod_vhost_cdb_set_script_alias(request_rec *r, apr_table_t *conf)
 {
-const char *script_alias=apr_table_get(conf,"ScriptAlias");
-if(script_alias==NULL)
+char *cgi=strstr(r->uri, "cgi-bin/");
+if (!cgi || (cgi && (cgi!=r->uri + strspn(r->uri, "/"))) )
 	{
-	ap_log_rerror(APLOG_MARK,APLOG_DEBUG,0,r,"mod_vhost_cdb_set_script_alias: no configured ScriptAlias");
 	return 0;
 	}
 
-char *cgi=strstr(r->uri, "cgi-bin/");
-if (!cgi || (cgi && (cgi!=r->uri + strspn(r->uri, "/"))) )
+const char *script_alias=apr_table_get(conf,"ScriptAlias");
+if(script_alias==NULL)
 	{
+	ap_log_rerror(APLOG_MARK,APLOG_DEBUG,0,r,"mod_vhost_cdb_set_script_alias: no configured ScriptAlias");
 	return 0;
 	}
 
@@ -228,7 +249,7 @@
 /* is configured to do so via VhostCDB_Export*					*/
 /* ****************************************************************************	*/
 
-int mod_vhost_cdb_export_vars(request_rec *r, apr_table_t *db_conf)
+static int mod_vhost_cdb_export_vars(request_rec *r, apr_table_t *db_conf)
 {
 mod_vhost_cdb_config *conf=(mod_vhost_cdb_config *) ap_get_module_config(r->server->module_config,&vhost_cdb_module);
 int itr;
@@ -236,7 +257,7 @@
 /* 
  * 1. Environment
  */
-if( (conf->export_to_env!=NULL) && !apr_is_empty_array(conf->export_to_env))
+if(conf->export_to_env!=NULL)
 	{
 	for (itr=0; itr < conf->export_to_env->nelts; itr++) 
 		{
@@ -254,7 +275,7 @@
  * 2. Notes
  */
 
-if( (conf->export_to_notes!=NULL) && !apr_is_empty_array(conf->export_to_notes))
+if(conf->export_to_notes!=NULL)
 	{
 	for (itr=0; itr < conf->export_to_notes->nelts; itr++) 
 		{
@@ -303,20 +324,16 @@
 /* ****************************************************************************	*/
 /* get_config_from_db(struct cdb *db, const char *hostname)			*/
 /* returns the entry from the db						*/
+/* this is only called when the hostname was not found at the first query	*/
 /* ****************************************************************************	*/
 
-static char *get_config_from_db(request_rec *r,struct cdb *db, const char *hostname, mod_vhost_cdb_config *conf)
+static char *get_config_from_db(request_rec *r, struct cdb *db, const char *hostname, mod_vhost_cdb_config *conf)
 {
-char *db_value=db_get(r, db, hostname);
-if(db_value)
-	{
-	return db_value;
-	}
-
 /* 
  * try to match foo.domain.tld -> *.domain.tld
  */
 char *dot_pos=(char *) hostname;
+char *db_value=NULL;
 
 while( (dot_pos!=NULL) && (db_value==NULL) )
 	{
@@ -392,8 +409,9 @@
 			}
 		/* ap_log_error(APLOG_MARK, APLOG_WARNING, 0,r->server, "trans_uri: using defaulthost \"%s\"",conf->defaulthost); */
 		hostname=conf->defaulthost;
+		r->hostname=apr_pstrdup(r->pool,conf->defaulthost);
 		}
-	};
+	}
 
 /*
  * open connection to db
@@ -414,10 +432,18 @@
 	}
 
 /*
- * now query db and close link afterwards
+ * try first query here, only call the more complicated
+ * stuff if nothing was found in the first attempt
  */
+char *db_value=db_get(r, db, hostname);
+if(!db_value)
+	{
+	db_value=get_config_from_db(r, db, hostname, conf);
+	}
 
-char *db_value=get_config_from_db(r, db, hostname, conf);
+/*
+ * close link
+ */
 cdb_free(db);
 close(fd);
 
@@ -440,16 +466,17 @@
 	char *redir_url;
 	if((redir_url=(char *)apr_table_get(db_conf,"Redirect"))==NULL)
 		{
-		ap_log_error(APLOG_MARK, APLOG_WARN, 0,r->server, "trans_uri: no DocumentRoot or Redirect configured for \"%s\"",hostname);
+		ap_log_error(APLOG_MARK, APLOG_WARNING, 0,r->server, "trans_uri: no DocumentRoot or Redirect configured for \"%s\"",hostname);
 		return DECLINED;
 		}
 	/* ap_log_error(APLOG_MARK, APLOG_DEBUG, 0,r->server, "trans_uri: redirect to \"%s\"",redir_url); */
 	apr_table_set(r->headers_out,"Location",redir_url);
 	return HTTP_MOVED_PERMANENTLY;
 	}
-
-/* if we came from a *.foobar.baz, then replace %1 in DocumentRoot 
- * and ScriptAlias with virtual_dir
+	
+/*
+ * if we came from a *.foobar.baz, then replace %1 in DocumentRoot 
+ * and ScriptAlias with the value of VHostCDB_DomainPrefix
  */
 if(apr_table_get(r->notes,"VHostCDB_DomainPrefix"))
 	{
@@ -464,7 +491,11 @@
 	return DECLINED;
 	}
 
+/*
+ * export vars if needed 
+ */
 mod_vhost_cdb_export_vars(r,db_conf);
+
 	
 /*
  * user and group have to be saved in request->notes, because
@@ -548,8 +579,8 @@
 
 conf->filename=apr_pstrcat(p,"/usr/local/etc/apache2/vhosts.cdb",NULL);
 conf->enable=0;
-conf->export_to_env=apr_array_make(p,0,sizeof(char *));
-conf->export_to_notes=apr_array_make(p,0,sizeof(char *));
+conf->export_to_env=NULL;
+conf->export_to_notes=NULL;
 conf->defaulthost=NULL;
 
 /* ap_log_error(APLOG_MARK, APLOG_DEBUG, 0,s, "mod_vhost_cdb: created vhost-config"); */
@@ -609,6 +640,11 @@
 {
 mod_vhost_cdb_config *conf = (mod_vhost_cdb_config *)ap_get_module_config(cmd->server->module_config, &vhost_cdb_module);
 
+if(!conf->export_to_env)
+	{
+	conf->export_to_env=apr_array_make(cmd->pool,1,sizeof(char *));
+	}
+
 char **ptr=apr_array_push(conf->export_to_env);
 *ptr=apr_pstrdup(cmd->pool,key);
 
@@ -626,6 +662,11 @@
 {
 mod_vhost_cdb_config *conf = (mod_vhost_cdb_config *)ap_get_module_config(cmd->server->module_config, &vhost_cdb_module);
 
+if(!conf->export_to_notes)
+	{
+	conf->export_to_notes=apr_array_make(cmd->pool,1,sizeof(char *));
+	}
+
 char **ptr=apr_array_push(conf->export_to_notes);
 *ptr=apr_pstrdup(cmd->pool,key);
 
