Answers.Body: <p>Cache poisoning would only happen if you don't <em>hard-set</em> your <code>@web</code> alias manually and it is auto-determined.</p>
  <p>Set it in your <code>config/general.php</code> with:</p>
  <pre class="lang-php prettyprint-override"><code>'aliases' =&gt; [
      'web' =&gt; getenv('SITE_BASE_URL'),
  ],
  </code></pre>
  <p>Then you'll be able to revert to using it in your Control Panel.</p>
  <p>In terms of why this happened; you can't <em>add</em> to an environment variable the way you can with an alias so <code>http://$SITE_BASE_URL/en</code> will become a string and the variable aspect of it will disappear.</p>
  <p>When you define <code>$SITE_BASE_URL</code>, it can only be used <em>as is</em> in the CP so if you wanted to use variables for you site, you would need to define one for each site/language.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-11-17T14:15:45.510Z
Answers.Text: Thanks, that seems to do the trick! I'd still like to know what the issue is with using the env variable, though...
Answers.UserDisplayName: idix
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-11-17T14:32:39.507Z
Answers.Text: @idix - added above
Answers.UserDisplayName: Oli
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-11-17T14:36:31.460Z
Answers.Text: Okay, that makes it clear to me, thanks. Is this explained somewhere in the docs? Up until now, I haven't seen it mentioned I think (at least not in the clear way you just explained it).
Answers.UserDisplayName: idix
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-11-17T14:56:11.473Z
Answers.Text: [The first sentence here](https://craftcms.com/docs/3.x/config/#using-aliases-in-control-panel-settings) touches on it but it's not covered in details.
Answers.UserDisplayName: Oli
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-11-17T14:02:19.583Z
Answers.LastActivityDate: 2020-11-17T14:32:21.323Z
Answers.LastEditDate: 2020-11-17T14:32:21.323Z
Answers.UserDisplayName: Oli
Body: <p>I'm working on a multi-site setup (for a localized website). In my <code>.env</code> file, I have defined a <code>SITE_BASE_URL</code> variable. Then, in the control panel, I set up each site's base url with that environment variable (e.g. <code>http://$SITE_BASE_URL/en</code>, <code>http://$SITE_BASE_URL/nl</code>, etc. for each language).</p>
  <p>However, when I need to generate urls in my Twig templates, this environment variable doesn't seem to get parsed; I get generated html that looks like this <code>&lt;a href=&quot;http://$SITE_BASE_URL/en/services&quot;&gt;...&lt;/a&gt;</code>.</p>
  <p>Example from a twig template:</p>
  <pre><code>{% set pages = [
      craft.entries.section(&quot;services&quot;).one(),
      craft.entries.section(&quot;products&quot;).one(),
  ] %}
  
  {% for page in pages %}
  &lt;a href=&quot;{{ page.url }}&quot;&gt;{{page.title}}&lt;/a&gt;
  {% endfor %}
  </code></pre>
  <p>This gives me URLs like <code>http://$SITE_BASE_URL/en/services</code>, <code>http://$SITE_BASE_URL/en/products</code>, etc.</p>
  <p>This setup was working fine for me when using the <code>@web</code> alias in the CP. Why doesn't it work with the env variable? The only reason I switched to env variables is that it is not recommended to use the <code>@web</code> alias due to cache poisoning.</p>
  <p>I've already looked at <a href="https://github.com/craftcms/cms/issues/3719" rel="nofollow noreferrer">https://github.com/craftcms/cms/issues/3719</a> and <a href="https://craftcms.stackexchange.com/questions/29490/baseurl-outputs-environment-variable">baseUrl outputs environment variable</a></p>
  <p>Neither have solved my issue...</p>
  
Tags: <craft3><localization><url><env>
Title: Site url is not generated correctly when using environment variables
ThreadText: # Site url is not generated correctly when using environment variables
  
  (Asked by: username_0 on 2020-11-17)
  
  I'm working on a multi-site setup (for a localized website). In my `.env` file, I have defined a `SITE_BASE_URL` variable. Then, in the control panel, I set up each site's base url with that environment variable (e.g. `http://$SITE_BASE_URL/en`, `http://$SITE_BASE_URL/nl`, etc. for each language).
  
  However, when I need to generate urls in my Twig templates, this environment variable doesn't seem to get parsed; I get generated html that looks like this `<a href="http://$SITE_BASE_URL/en/services">...</a>`.
  
  Example from a twig template:
  
  ```
  {% set pages = [
      craft.entries.section("services").one(),
      craft.entries.section("products").one(),
  ] %}
  
  {% for page in pages %}
  <a href="{{ page.url }}">{{page.title}}</a>
  {% endfor %}
  
  ```
  
  This gives me URLs like `http://$SITE_BASE_URL/en/services`, `http://$SITE_BASE_URL/en/products`, etc.
  
  This setup was working fine for me when using the `@web` alias in the CP. Why doesn't it work with the env variable? The only reason I switched to env variables is that it is not recommended to use the `@web` alias due to cache poisoning.
  
  I've already looked at <https://github.com/craftcms/cms/issues/3719> and [baseUrl outputs environment variable](https://craftcms.stackexchange.com/questions/29490/baseurl-outputs-environment-variable)
  
  Neither have solved my issue...
  
  **Tags:** craft3, localization, url, env
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2020-11-17. Score: 3 | Accepted answer
  
  Cache poisoning would only happen if you don't *hard-set* your `@web` alias manually and it is auto-determined.
  
  Set it in your `config/general.php` with:
  
  ```php
  'aliases' => [
      'web' => getenv('SITE_BASE_URL'),
  ],
  
  ```
  
  Then you'll be able to revert to using it in your Control Panel.
  
  In terms of why this happened; you can't *add* to an environment variable the way you can with an alias so `http://$SITE_BASE_URL/en` will become a string and the variable aspect of it will disappear.
  
  When you define `$SITE_BASE_URL`, it can only be used *as is* in the CP so if you wanted to use variables for you site, you would need to define one for each site/language.
  
  > **Comments:**
  > **username_0**: Thanks, that seems to do the trick! I'd still like to know what the issue is with using the env variable, though...  
  > **username_1**: @username_0 - added above  
  > **username_0**: Okay, that makes it clear to me, thanks. Is this explained somewhere in the docs? Up until now, I haven't seen it mentioned I think (at least not in the clear way you just explained it).  
  > **username_1**: [The first sentence here](https://craftcms.com/docs/3.x/config/#using-aliases-in-control-panel-settings) touches on it but it's not covered in details.  
  
  ---
  
  