<p>What I like to do is explicitly set <code>CRAFT_ENVIRONMENT</code> in my index.php:</p>

<pre><code>switch($_SERVER['SERVER_NAME']) {
  case 'domain.dev':
    define('CRAFT_ENVIRONMENT', 'LOCAL');
    break;
  case 'staging.domain.com':
    define('CRAFT_ENVIRONMENT', 'STAGING');
    break;
  case 'www.domain.com':
  case 'domain.com':
    define('CRAFT_ENVIRONMENT', 'PRODUCTION');
    break;
}
</code></pre>

<p>Then in the config file:</p>

<pre><code>return array(
  '*' =&gt; array(
    // Settings here
  ),
  'LOCAL' =&gt; array(
    // Settings here
  ),
  'STAGING' =&gt; array(
    // Settings here
  ),
  'PRODUCTION' =&gt; array(
    // Settings here
  )
);
</code></pre>

<p>When you set <code>CRAFT_ENVIRONMENT</code> you may want to use the switch default for <code>LOCAL</code> or for <code>PRODUCTION</code> - there are good arguments for going either way.</p>

<p>A separate question is whether you <em>should</em> have people accessing your site at both <code>www.domain.com</code> and <code>domain.com</code>. The answer is most probably <strong>no</strong>. Pick one, and use .htaccess (on apache) or whatever other method, to redirect either all the www to non-www or the other way around. Otherwise you will have duplicate content, which confuses the search engines (and generally looks unprofessional).</p>

CC BY-SA 3.0
2014-12-03T20:25:34.760Z
Thanks for the tip Marion. If I wanted to use the switch default for `PRODUCTION` would I write it like this:

`default:
    define('CRAFT_ENVIRONMENT', 'PRODUCTION');
    break;
}`
?
Adam Snetman
CC BY-SA 3.0
2014-12-03T20:27:30.400Z
Sorry for the nonexistent multi-line formatting. Hope you can make sense of that.
Adam Snetman
CC BY-SA 3.0
2014-12-03T20:35:10.300Z
yes, exactly...
Marion Newlevant
CC BY-SA 3.0
2014-12-03T19:32:50.610Z
2014-12-03T20:46:58.373Z
2014-12-03T20:46:58.373Z
Marion Newlevant
<p>How can I cover both "www.domain.com" and "domain.com" with the same settings in my config file? My current file is set up like this:</p>

<pre><code>return array(
    '*' =&gt; array(
       // Settings here
    ),

    // Local
    'domain.dev' =&gt; array(
       // Settings here
    ),

    // Staging
    'staging.domain.com' =&gt; array(
        // Settings here
    ),

    // Production
    'www.domain.com' =&gt; array(
       // Settings here
    )
);
</code></pre>

<p>Can I just remove the <code>www</code> from the last entry under “Production” to have those settings apply to both the naked domain and the <code>www</code> subdomain?</p>

<p>And if I'm including the <code>www</code> in my <code>siteUrl</code> config setting in Production, will that create any conflicts if visitors end up at a naked url without the <code>www</code> subdomain specified?</p>

<p>Thanks!</p>

<config><multi-environment>
Covering both "www.domain.com" and "domain.com" in config file settings
# Covering both "www.domain.com" and "domain.com" in config file settings

(Asked by: username_0 on 2014-12-03)

How can I cover both "www.domain.com" and "domain.com" with the same settings in my config file? My current file is set up like this:
return array(
    '*' => array(
       // Settings here
    ),

    // Local
    'domain.dev' => array(
       // Settings here
    ),

    // Staging
    'staging.domain.com' => array(
        // Settings here
    ),

    // Production
    'www.domain.com' => array(
       // Settings here
    )
);

Can I just remove the www from the last entry under “Production” to have those settings apply to both the naked domain and the www subdomain?
And if I'm including the www in my siteUrl config setting in Production, will that create any conflicts if visitors end up at a naked url without the www subdomain specified?
Thanks!

**Tags:** config, multi-environment

---

## 1 answers

### Answer by username_1 on 2014-12-03. Score: 4 | Accepted answer

What I like to do is explicitly set CRAFT_ENVIRONMENT in my index.php:
switch($_SERVER['SERVER_NAME']) {
  case 'domain.dev':
    define('CRAFT_ENVIRONMENT', 'LOCAL');
    break;
  case 'staging.domain.com':
    define('CRAFT_ENVIRONMENT', 'STAGING');
    break;
  case 'www.domain.com':
  case 'domain.com':
    define('CRAFT_ENVIRONMENT', 'PRODUCTION');
    break;
}

Then in the config file:
return array(
  '*' => array(
    // Settings here
  ),
  'LOCAL' => array(
    // Settings here
  ),
  'STAGING' => array(
    // Settings here
  ),
  'PRODUCTION' => array(
    // Settings here
  )
);

When you set CRAFT_ENVIRONMENT you may want to use the switch default for LOCAL or for PRODUCTION - there are good arguments for going either way.
A separate question is whether you should have people accessing your site at both www.domain.com and domain.com. The answer is most probably no. Pick one, and use .htaccess (on apache) or whatever other method, to redirect either all the www to non-www or the other way around. Otherwise you will have duplicate content, which confuses the search engines (and generally looks unprofessional).

> **Comments:**
> **username_0**: Thanks for the tip Marion. If I wanted to use the switch default for `PRODUCTION` would I write it like this:  `default:     define('CRAFT_ENVIRONMENT', 'PRODUCTION');     break; }` ?  
> **username_0**: Sorry for the nonexistent multi-line formatting. Hope you can make sense of that.  
> **username_1**: yes, exactly...  

---

