Answers.Body: <p>Good practice imo, it's what I do as well. Values come from the <code>.env</code> vars, and for things that are URLs or paths, I make aliases out of them to take advantage of what you're showing above.</p>
  
  <p>This cannot cause any kind of issue like <code>@web</code> -- the only thing wrong with <code>@web</code> is that the host name is computed dynamically, potentially from untrusted browser data.</p>
  
  <p>You're not doing that here, you are explicitly providing the host name, so it's totally fine. You could even do:</p>
  
  <pre class="lang-php prettyprint-override"><code>'aliases' =&gt; [
          '@web' =&gt; getenv('ROOT_URL'),
      ],
  
  </code></pre>
  
  <p>...to redefine the dynamically generated <code>@web</code> alias to something that is from a static value for the URL.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-03-24T15:52:59.573Z
Answers.LastActivityDate: 2019-03-24T15:52:59.573Z
Answers.UserDisplayName: andrew.welch
Body: <p>I was wondering if the following setup is a good practice.</p>
  
  <p>In my .env file I defined a single rootUrl var which contains the site root url like this:</p>
  
  <p><code>ROOT_URL="https://www.mydomain.test"</code></p>
  
  <p>In the general.php config file i've created an alias (@rootUrl) that contains the ROOT_URL env var value:</p>
  
  <p><code>'aliases' =&gt; [
          '@rootUrl' =&gt; getenv('ROOT_URL'),
      ],</code></p>
  
  <p>Then I define my craft sites as following (multilingual site), each site BASE URL is constructed with the rootUrl and a language:</p>
  
  <p><img src="https://user-images.githubusercontent.com/1284055/54807616-185f4d80-4c7e-11e9-9c3c-45d8954ed3f3.png" alt="Schermafbeelding 2019-03-22 om 08 39 32"></p>
  
  <p>Advantages</p>
  
  <ul>
  <li>You don't have to define every site (language) in the code, languages are managed through the cms (in case of multisite setup, you should define a ROOT_URL for every site).</li>
  <li>the @rootUrl alias can be used as part of the resource path in the templates.</li>
  </ul>
  
  <p>Disadvantages</p>
  
  <ul>
  <li>Could this also resolve in a caching issue (<a href="https://twitter.com/nystudio107/status/1074429502615379968" rel="nofollow noreferrer">like the @web alias</a>)?</li>
  <li>...</li>
  </ul>
  
  <p>Does this logic makes sense or is it generally a bad idea to use aliases for the site BASE URL setting?</p>
  
  <p>Thanks in advance</p>
  
Tags: <config><siteurl><setup>
Title: Use alias for site url based on root url defined as env var?
ThreadText: # Use alias for site url based on root url defined as env var?
  
  (Asked by: username_0 on 2019-03-23)
  
  I was wondering if the following setup is a good practice.
  
  In my .env file I defined a single rootUrl var which contains the site root url like this:
  
  `ROOT_URL="https://www.mydomain.test"`
  
  In the general.php config file i've created an alias (@rootUrl) that contains the ROOT\_URL env var value:
  
  `'aliases' => [
  '@rootUrl' => getenv('ROOT_URL'),
  ],`
  
  Then I define my craft sites as following (multilingual site), each site BASE URL is constructed with the rootUrl and a language:
  
  ![Schermafbeelding 2019-03-22 om 08 39 32](https://user-images.githubusercontent.com/1284055/54807616-185f4d80-4c7e-11e9-9c3c-45d8954ed3f3.png)
  
  Advantages
  
  * You don't have to define every site (language) in the code, languages are managed through the cms (in case of multisite setup, you should define a ROOT\_URL for every site).
  * the @rootUrl alias can be used as part of the resource path in the templates.
  
  Disadvantages
  
  * Could this also resolve in a caching issue ([like the @web alias](https://twitter.com/nystudio107/status/1074429502615379968))?
  * ...
  
  Does this logic makes sense or is it generally a bad idea to use aliases for the site BASE URL setting?
  
  Thanks in advance
  
  **Tags:** config, siteurl, setup
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2019-03-24. Score: 2
  
  Good practice imo, it's what I do as well. Values come from the `.env` vars, and for things that are URLs or paths, I make aliases out of them to take advantage of what you're showing above.
  
  This cannot cause any kind of issue like `@web` -- the only thing wrong with `@web` is that the host name is computed dynamically, potentially from untrusted browser data.
  
  You're not doing that here, you are explicitly providing the host name, so it's totally fine. You could even do:
  
  ```php
  'aliases' => [
          '@web' => getenv('ROOT_URL'),
      ],
  
  ```
  
  ...to redefine the dynamically generated `@web` alias to something that is from a static value for the URL.
  
  
  ---
  
  