have you tried switch statment in wp-config?
/* URLS - not used anymore - doesn't work in Multidomain system*/
define('WP_HOME', 'http://'.str_replace("www.","", $_SERVER['HTTP_HOST']));
define('WP_SITEURL', 'http://'.str_replace("www.","", $_SERVER['HTTP_HOST']));
switch(str_replace('http://', '', WP_HOME)){
case 'dev.domain.com':
define('DB_NAME', 'db1_dev');
define('DB_USER', 'db1_dev_user');
define('DB_PASSWORD', 'db1_dev_password');
define('DB_HOST', 'db1_dev_hostname');
define('MEDIAURL', 'http://media.'.WP_HOME);
define('MEDIAPATH', '/var/www/media'.WP_HOME);
break;
case 'stage.domain.com':
define('DB_NAME', 'db2_stage');
define('DB_USER', 'db2_stage_user');
define('DB_PASSWORD', 'db2_stage_password');
define('DB_HOST', 'db2_stage_hostname');
define('MEDIAURL', 'http://media.'.WP_HOME);
define('MEDIAPATH', '/var/www/media'.WP_HOME);
break;
case 'domain.com':
define('DB_NAME', 'db3_production');
define('DB_USER', 'db3_production_user');
define('DB_PASSWORD', 'db3_production_password');
define('DB_HOST', 'db3_production_hostname');
define('MEDIAURL', 'http://media.'.WP_HOME);
define('MEDIAPATH', '/var/www/media'.WP_HOME);
break;
}