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 = [
] %}

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

Neither have solved my issue...

1 Answer 1


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.

  • Thanks, that seems to do the trick! I'd still like to know what the issue is with using the env variable, though...
    – idix
    Nov 17, 2020 at 14:15
  • @idix - added above
    – Oli
    Nov 17, 2020 at 14:32
  • 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
    Nov 17, 2020 at 14:36
  • The first sentence here touches on it but it's not covered in details.
    – Oli
    Nov 17, 2020 at 14:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.