<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>

CC BY-SA 4.0
2020-11-17T14:15:45.510Z
Thanks, that seems to do the trick! I'd still like to know what the issue is with using the env variable, though...
idix
CC BY-SA 4.0
2020-11-17T14:32:39.507Z
@idix - added above
Oli
CC BY-SA 4.0
2020-11-17T14:36:31.460Z
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).
idix
CC BY-SA 4.0
2020-11-17T14:56:11.473Z
[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.
Oli
CC BY-SA 4.0
2020-11-17T14:02:19.583Z
2020-11-17T14:32:21.323Z
2020-11-17T14:32:21.323Z
Oli
<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>

<craft3><localization><url><env>
Site url is not generated correctly when using environment variables
# 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 ....
Example from a twig template:
{% set pages = [
    craft.entries.section("services").one(),
    craft.entries.section("products").one(),
] %}

{% for page in pages %}
{{page.title}}
{% 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
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:
'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**: @idix - 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.  

---

