<p>Aliases can be defined and/or overridden using the <a href="https://craftcms.com/docs/3.x/config/config-settings.html#aliases" rel="nofollow noreferrer"><code>aliases</code></a> config setting from your <code>config/general.php</code> file.</p>
<p>Assuming you have your site URL set to an environment variable <code>PRIMARY_SITE_URL</code> or similar:</p>
<pre class="lang-php prettyprint-override"><code>use craft\helpers\App;

return [
    ...
    'aliases' =&gt; [
        '@web' =&gt; App::env('PRIMARY_SITE_URL'),
    ],
];
</code></pre>
<p>This is also referenced in the official docs <a href="https://craftcms.com/docs/3.x/config/#aliases" rel="nofollow noreferrer">here</a>.</p>

CC BY-SA 4.0
2022-04-21T16:29:03.760Z
Great! I got it working, using your syntax. Thanks a lot! :)
qbit76
CC BY-SA 4.0
2022-04-22T07:45:10.587Z
Just for additional context as it's a little buried in the docs, the recommendation of explicitly setting the @web alias is mainly around protecting against a potential cache poisoning attack vector. By setting this alias you are ensuring only a specific site URL can be returned.
James White
CC BY-SA 4.0
2022-04-21T16:24:10.437Z
2022-04-21T16:24:10.437Z
Mats Mikkel Rummelhoff
<h1>Update for Craft CMS V4</h1>
<p><strong>Note</strong>: <em>You can still use the 'older' V3 syntax in V4, see the comment below.</em></p>
<hr />
<p>For version four, the syntax for general config has changed.<br />
<a href="https://craftcms.com/docs/4.x/config/general.html#system" rel="nofollow noreferrer">See the docs</a></p>
<pre class="lang-php prettyprint-override"><code>// config/general.php

return GeneralConfig::create()
    ...
    // Set aliases
    -&gt;aliases([
        '@web' =&gt; App::env('PRIMARY_SITE_URL'),
    ])
;
</code></pre>
<p><em>For V3, use <a href="https://craftcms.stackexchange.com/a/39347/13762">the accepted answer</a> by Mats Mikkel Rummelhoff</em></p>

CC BY-SA 4.0
2023-01-10T16:57:06.730Z
Technically, the old (array) syntax still works in Craft 4 and there are no plans to remove it from future Craft versions – but the new, "fluent" syntax outlined here is the one Pixel & Tonic is promoting currently :)
Mats Mikkel Rummelhoff
CC BY-SA 4.0
2023-01-11T08:28:38.197Z
@MatsMikkelRummelhoff Ah, that makes sense. I'm relatively new to Craft and noticed the syntax in the default templates that got created were different to any shown here. I'll update the answer! Thanks!
Kerwin Sneijders
CC BY-SA 4.0
2023-01-10T14:59:14.647Z
2023-01-12T11:43:37.017Z
2023-01-12T11:43:37.017Z
Kerwin Sneijders
<p>In case of multi-environment config,
You must paste the Mats's answer into '*' key (config/general.php), like this:</p>
<pre><code>return [
    '*' =&gt; [
        'aliases' =&gt; [
            '@web' =&gt; App::env('PRIMARY_SITE_URL'),
        ],
        ...
    ],
];
</code></pre>
<p>I had to read the core to found how silly is this undocumented setting!</p>

CC BY-SA 4.0
2022-07-21T10:25:54.667Z
2022-07-21T10:25:54.667Z
Cskt Coop
<p>I just stumbled upon this advice in my Craft3-system report:</p>
<p><em>&quot;Ensure @web alias is explicitly overridden&quot;</em></p>
<p>In which file do I need to put the override? &quot;config/general.php&quot; doesn't seem to work... </p>

<craft3><alias>
Ensure @web alias is explicitly overridden
# Ensure @web alias is explicitly overridden

(Asked by: username_0 on 2022-04-21)

I just stumbled upon this advice in my Craft3-system report:

*"Ensure @web alias is explicitly overridden"*

In which file do I need to put the override? "config/general.php" doesn't seem to work...

**Tags:** craft3, alias

---

## 3 answers

### Answer by username_1 on 2022-04-21. Score: 3 | Accepted answer

Aliases can be defined and/or overridden using the aliases config setting from your config/general.php file.
Assuming you have your site URL set to an environment variable PRIMARY_SITE_URL or similar:
use craft\helpers\App;

return [
    ...
    'aliases' => [
        '@web' => App::env('PRIMARY_SITE_URL'),
    ],
];

This is also referenced in the official docs here.

> **Comments:**
> **username_0**: Great! I got it working, using your syntax. Thanks a lot! :)  
> **username_2**: Just for additional context as it's a little buried in the docs, the recommendation of explicitly setting the @web alias is mainly around protecting against a potential cache poisoning attack vector. By setting this alias you are ensuring only a specific site URL can be returned.  

---

### Answer by username_3 on 2023-01-10. Score: 5

Update for Craft CMS V4
Note: You can still use the 'older' V3 syntax in V4, see the comment below.

For version four, the syntax for general config has changed.
See the docs
// config/general.php

return GeneralConfig::create()
    ...
    // Set aliases
    ->aliases([
        '@web' => App::env('PRIMARY_SITE_URL'),
    ])
;

For V3, use the accepted answer by Mats Mikkel Rummelhoff

> **Comments:**
> **username_1**: Technically, the old (array) syntax still works in Craft 4 and there are no plans to remove it from future Craft versions – but the new, "fluent" syntax outlined here is the one Pixel & Tonic is promoting currently :)  
> **username_3**: @MatsMikkelRummelhoff Ah, that makes sense. I'm relatively new to Craft and noticed the syntax in the default templates that got created were different to any shown here. I'll update the answer! Thanks!  

---

### Answer by username_4 on 2022-07-21. Score: 2

In case of multi-environment config,
You must paste the Mats's answer into '*' key (config/general.php), like this:
return [
    '*' => [
        'aliases' => [
            '@web' => App::env('PRIMARY_SITE_URL'),
        ],
        ...
    ],
];

I had to read the core to found how silly is this undocumented setting!


---

