Answers.Body: <p>As long as you have the more specific one at the top of your array like you do, it should work.</p>
  
  <p>It looks like your using some custom (non-stock) Craft config logic judging from the <code>$customDbConfig</code> variable.</p>
  
  <p>What happens if you <code>echo $_SERVER['SERVER_NAME'];</code> from the test1.foo.com domain?</p>
  
  <p>If it's an exact match and it is still not selecting the right database, then I'd suspect your custom logic.</p>
  
  <p>Strip all of that out to just Craft's default multi-environment config and see if it works that way. </p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-12-12T20:33:13.747Z
Answers.Text: Thanks Brad, I ended up just naming each env based on SERVER_NAME in the public index, and adding configs based on those. Pretty much what the folks at Barrel Strength do: https://github.com/BarrelStrength/Craft-Master/blob/master/public/index.php
Answers.UserDisplayName: Iain Urquhart
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-12-12T04:48:11.577Z
Answers.LastActivityDate: 2014-12-12T04:48:11.577Z
Answers.UserDisplayName: Brad Bell
Body: <p>So I have this example database config setup</p>
  
  <pre><code>$customDbConfig = array(
  
      '*' =&gt; array(
          'tablePrefix' =&gt; 'craft',
      ),
  
      'test1.foo.com' =&gt; array(
          'server' =&gt; 'localhost',
          'user' =&gt; 'test1user',
          'password' =&gt; '',
          'database' =&gt; '',
      ),
  
      'test2.foo.com' =&gt; array(
          'server' =&gt; 'localhost',
          'user' =&gt; 'test2user',
          'password' =&gt; '',
          'database' =&gt; '',
      ),
  
      'foo.com' =&gt; array(
          'server' =&gt; 'localhost',
          'user' =&gt; 'produser',
          'password' =&gt; '',
          'database' =&gt; '',
      ),
  
  );
  </code></pre>
  
  <p>It appears that when accessing test1.foo.com, the foo.com environment is being loaded. </p>
  
  <p>Looking at the docs again</p>
  
  <blockquote>
    <p>When Craft is comparing your config keys with $_SERVER['SERVER_NAME'],
    it’s only looking for a partial match. So the environment key
    'example.com' will also work if you’re accessing your site via
    www.example.com or any other subdomain. You could even just use the
    TLD if you’re feeling adventurous:</p>
  </blockquote>
  
  <p>What's the best way to get around this issue, where we want subdomains for dev and test, when they are subdomains of the production domain.</p>
  
  <p>Thanks.</p>
  
Tags: <config><database><subdomain><configuration>
Title: Subdomain database configuration picking up root domain config
ThreadText: # Subdomain database configuration picking up root domain config
  
  (Asked by: username_0 on 2014-12-11)
  
  So I have this example database config setup
  
  ```
  $customDbConfig = array(
  
      '*' => array(
          'tablePrefix' => 'craft',
      ),
  
      'test1.foo.com' => array(
          'server' => 'localhost',
          'user' => 'test1user',
          'password' => '',
          'database' => '',
      ),
  
      'test2.foo.com' => array(
          'server' => 'localhost',
          'user' => 'test2user',
          'password' => '',
          'database' => '',
      ),
  
      'foo.com' => array(
          'server' => 'localhost',
          'user' => 'produser',
          'password' => '',
          'database' => '',
      ),
  
  );
  
  ```
  
  It appears that when accessing test1.foo.com, the foo.com environment is being loaded.
  
  Looking at the docs again
  
  > When Craft is comparing your config keys with $\_SERVER['SERVER\_NAME'],
  > it’s only looking for a partial match. So the environment key
  > 'example.com' will also work if you’re accessing your site via
  > www.example.com or any other subdomain. You could even just use the
  > TLD if you’re feeling adventurous:
  
  What's the best way to get around this issue, where we want subdomains for dev and test, when they are subdomains of the production domain.
  
  Thanks.
  
  **Tags:** config, database, subdomain, configuration
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2014-12-12. Score: 0 | Accepted answer
  
  As long as you have the more specific one at the top of your array like you do, it should work.
  
  It looks like your using some custom (non-stock) Craft config logic judging from the `$customDbConfig` variable.
  
  What happens if you `echo $_SERVER['SERVER_NAME'];` from the test1.foo.com domain?
  
  If it's an exact match and it is still not selecting the right database, then I'd suspect your custom logic.
  
  Strip all of that out to just Craft's default multi-environment config and see if it works that way.
  
  > **Comments:**
  > **username_0**: Thanks username_1, I ended up just naming each env based on SERVER_NAME in the public index, and adding configs based on those. Pretty much what the folks at Barrel Strength do: https://github.com/BarrelStrength/Craft-Master/blob/master/public/index.php  
  
  ---
  
  