Drupal配置文件settings.php搜索规则

Drupal的配置文件搜索是通过bootstrap.inc的conf_path()函数实现的:

function conf_path($require_settings = TRUE, $reset = FALSE) {
  $conf = &drupal_static(__FUNCTION__, '');

  if ($conf && !$reset) {
    return $conf;
  }

  $confdir = 'sites';

  $sites = array();
  if (file_exists(DRUPAL_ROOT . '/' . $confdir . '/sites.php')) {
    // This will overwrite $sites with the desired mappings.
    include(DRUPAL_ROOT . '/' . $confdir . '/sites.php');
  }

  $uri = explode('/', $_SERVER['SCRIPT_NAME'] ? $_SERVER['SCRIPT_NAME'] : $_SERVER['SCRIPT_FILENAME']);
  $server = explode('.', implode('.', array_reverse(explode(':', rtrim($_SERVER['HTTP_HOST'], '.')))));
  for ($i = count($uri) - 1; $i > 0; $i--) {
    for ($j = count($server); $j > 0; $j--) {
      $dir = implode('.', array_slice($server, -$j)) . implode('.', array_slice($uri, 0, $i));
      if (isset($sites[$dir]) && file_exists(DRUPAL_ROOT . '/' . $confdir . '/' . $sites[$dir])) {
        $dir = $sites[$dir];
      }
      if (file_exists(DRUPAL_ROOT . '/' . $confdir . '/' . $dir . '/settings.php') || (!$require_settings && file_exists(DRUPAL_ROOT . '/' . $confdir . '/' . $dir))) {
        $conf = "$confdir/$dir";
        return $conf;
      }
    }
  }
  $conf = "$confdir/default";
  return $conf;
}

例如,对网址http://www.drupal.org:8080/mysite/test/的访问,Drupal会安装下面的顺序搜索配置文件:

sites/8080.www.drupal.org.mysite.test
sites/www.drupal.org.mysite.test
sites/drupal.org.mysite.test
sites/org.mysite.test

sites/8080.www.drupal.org.mysite
sites/www.drupal.org.mysite
sites/drupal.org.mysite
sites/org.mysite

sites/8080.www.drupal.org
sites/www.drupal.org
sites/drupal.org
sites/org

sites/default

sites/sites.php可以为站点设置别名。例如可以把访问http://www.drupal.org:8080/mysite/test/的配置文件重定向到example.com/settings.php:

$sites['8080.www.drupal.org.mysite.test'] = 'example.com';

参考:

Multisite - Sharing the same code base

原文地址:https://www.cnblogs.com/eastson/p/3351526.html