4

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

3 Answers 3

3

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.

2
  • 1
    Great! I got it working, using your syntax. Thanks a lot! :)
    – qbit76
    Commented Apr 21, 2022 at 16:29
  • 1
    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. Commented Apr 22, 2022 at 7:45
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

2
  • 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 :) Commented Jan 10, 2023 at 16:57
  • @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! Commented Jan 11, 2023 at 8:28
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!

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.