Craft 3: how can I set dynamic Base URL in sites setting? Sites localized versions are setup using Base URL like this:

— http://{baseUrl}/en/
— http://{baseUrl}/fr/


But {baseUrl} variables are not transformed using craft.app.sites.getAllSites(). 

{% set sites = craft.app.sites.getAllSites() %}
{% for site in sites %}
    {{ site.baseUrl }}
    // → outputs http://{baseUrl}/locale/ instead of http://mywebsite.com/en/
{% endfor %}


Whereas baseUrl are set from general.php:

'aliases' =&gt; array(
    'basePath' =&gt; '/mywebsite.com/htdocs/',
    'baseUrl'  =&gt; 'http://www.mywebsite.com/'
),
So as of Craft CMS 3 RC7, they added a new alias() Twig function.

So what you can do is something like this in your general.php:

'aliases' =&gt; [
    '@basePath' =&gt; getenv('CRAFTENV_BASE_PATH'),
    '@baseUrl' =&gt; getenv('CRAFTENV_BASE_URL'),
],


(or whatever/however you want to set them). These aliases can be used in sites’ Base URL settings, volumes’ Base URL settings, and Local volumes’ File System Path settings in the AdminCP.

And then in your templates, you can do something like:

{{ alias('@baseUrl') }}
{{ alias('@baseUrl/assets/img/' }}


To resolve the alias there. I changed Craft 3 Multi-Environment to leverage aliases in this way (rather than having them in the .custom config), and it's documented in detail in the Asset Volumes and Aliases.

You do not need to be using Craft 3 Multi-Environment to utilize them in the same way, this is just a live example that's handy to point to.