Answers.Body: <p>The <code>routes.php</code> config file doesn't support multi-environment setups in the manner that <code>general.php</code> or <code>db.php</code> does – in other words, your example code using a multidimensional array would throw an error.</p>
  
  <p>As a workaround, you can write some PHP in your <code>routes.php</code> file. For simple stuff, a ternary operator may suffice:</p>
  
  <pre><code>return array(
      'test' =&gt; strpos(CRAFT_ENVIRONMENT, '.loc') !== false ? '_test/index' : 'index', 
  );
  </code></pre>
  
  <p>For more complex stuff, you could set up different arrays of routes for different enviroments, and maybe do some merging:</p>
  
  <pre><code>$routes = array(
      'foo' =&gt; '_foo',
      'bar' =&gt; '_bar',
  );
  
  $devRoutes = array(
      'baz' =&gt; '_baz',
  );
  
  $stagingRoutes = array(
      'baz' =&gt; '_staging/baz',
  );
  
  if (strpos(CRAFT_ENVIRONMENT, '.loc') !== false) {
      $routes = array_merge($routes, $devRoutes);
  } else if (strpos(CRAFT_ENVIRONMENT, '.staging') !== false) {
      $routes = array_merge($routes, $stagingRoutes);
  }
  
  return $routes;
  </code></pre>
  
  <p>If you're wondering about the <a href="https://craftcms.com/docs/php-constants#craft-environment"><code>CRAFT_ENVIRONMENT</code></a> constant, that'll default to your hostname (e.g. <code>myawesomesite.loc</code>) unless you explicitly set it to something else before bootstrapping Craft (e.g. in your <code>index.php</code> file). The simple <code>strpos(CRAFT_ENVIRONMENT, $env)</code> test is how Craft merges multi-enviroment configs (e.g. from <code>general.php</code>) <a href="https://github.com/pixelandtonic/Craft-Release/blob/master/app/services/ConfigService.php#L861">under the hood</a>.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-07-24T12:19:00.250Z
Answers.Text: Thanks for the detailed answer; this works. I never even knew you could query the CRAFT_ENVIRONMENT like that. In practice I can see if this were used as a way to redirect users somewhere generic (like in my example), there would be no way to define a Canonical link within the template. So very bad for SEO. In very rare cases this might be handy; for now I'm just happy it is possible and clean to do. Thanks again.
Answers.UserDisplayName: Mark Notton
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-06-20T18:05:48.970Z
Answers.Text: Is `CRAFT_ENVIRONMENT` still the recommended way to get at this value in Craft 3? I see it does work and `craft.app.environment` doesn't…
Answers.UserDisplayName: henry
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-07-24T08:52:33.340Z
Answers.LastActivityDate: 2016-07-24T09:00:15.190Z
Answers.LastEditDate: 2016-07-24T09:00:15.190Z
Answers.UserDisplayName: Mats Mikkel Rummelhoff
Body: <p>In the general.php config file, I can target settings for multi-environments like so:</p>
  
  <pre><code>return array(
    '*' =&gt; array(
      'devMode' =&gt; false
    ),
  
    '.loc' =&gt; array(
      'devMode' =&gt; true
    )
  );
  </code></pre>
  
  <p><strong>Is it possible to do something similar in the routes.php file too?</strong></p>
  
  <pre><code>return array(
    '*' =&gt; array(
      'test' =&gt; 'index'
    ),
  
    '.loc' =&gt; array(
      'test' =&gt; '_test/index'
    )
  );
  </code></pre>
  
  <p>I don't want to differentiate the locales, but the environment instead. I know it's possible to query how content gets included within the templates themselves. I just wondered if this is possible instead... I don't actually have a use-case example. </p>
  
  <p>Maybe a time will arise where I want to allow certain content to be accessible for 'staging.' domains only. </p>
  
  <p>Thanks, </p>
  
  <p>Mark</p>
  
Tags: <routes><multi-environment>
Title: Is environmental routing possible?
ThreadText: # Is environmental routing possible?
  
  (Asked by: username_0 on 2016-07-23)
  
  In the general.php config file, I can target settings for multi-environments like so:
  
  ```
  return array(
    '*' => array(
      'devMode' => false
    ),
  
    '.loc' => array(
      'devMode' => true
    )
  );
  
  ```
  
  **Is it possible to do something similar in the routes.php file too?**
  
  ```
  return array(
    '*' => array(
      'test' => 'index'
    ),
  
    '.loc' => array(
      'test' => '_test/index'
    )
  );
  
  ```
  
  I don't want to differentiate the locales, but the environment instead. I know it's possible to query how content gets included within the templates themselves. I just wondered if this is possible instead... I don't actually have a use-case example.
  
  Maybe a time will arise where I want to allow certain content to be accessible for 'staging.' domains only.
  
  Thanks,
  
  Mark
  
  **Tags:** routes, multi-environment
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2016-07-24. Score: 6 | Accepted answer
  
  The `routes.php` config file doesn't support multi-environment setups in the manner that `general.php` or `db.php` does – in other words, your example code using a multidimensional array would throw an error.
  
  As a workaround, you can write some PHP in your `routes.php` file. For simple stuff, a ternary operator may suffice:
  
  ```
  return array(
      'test' => strpos(CRAFT_ENVIRONMENT, '.loc') !== false ? '_test/index' : 'index', 
  );
  
  ```
  
  For more complex stuff, you could set up different arrays of routes for different enviroments, and maybe do some merging:
  
  ```
  $routes = array(
      'foo' => '_foo',
      'bar' => '_bar',
  );
  
  $devRoutes = array(
      'baz' => '_baz',
  );
  
  $stagingRoutes = array(
      'baz' => '_staging/baz',
  );
  
  if (strpos(CRAFT_ENVIRONMENT, '.loc') !== false) {
      $routes = array_merge($routes, $devRoutes);
  } else if (strpos(CRAFT_ENVIRONMENT, '.staging') !== false) {
      $routes = array_merge($routes, $stagingRoutes);
  }
  
  return $routes;
  
  ```
  
  If you're wondering about the [`CRAFT_ENVIRONMENT`](https://craftcms.com/docs/php-constants#craft-environment) constant, that'll default to your hostname (e.g. `myawesomesite.loc`) unless you explicitly set it to something else before bootstrapping Craft (e.g. in your `index.php` file). The simple `strpos(CRAFT_ENVIRONMENT, $env)` test is how Craft merges multi-enviroment configs (e.g. from `general.php`) [under the hood](https://github.com/pixelandtonic/Craft-Release/blob/master/app/services/ConfigService.php#L861).
  
  > **Comments:**
  > **username_0**: Thanks for the detailed answer; this works. I never even knew you could query the CRAFT_ENVIRONMENT like that. In practice I can see if this were used as a way to redirect users somewhere generic (like in my example), there would be no way to define a Canonical link within the template. So very bad for SEO. In very rare cases this might be handy; for now I'm just happy it is possible and clean to do. Thanks again.  
  > **username_2**: Is `CRAFT_ENVIRONMENT` still the recommended way to get at this value in Craft 3? I see it does work and `craft.app.environment` doesn't…  
  
  ---
  
  