0

We're working a multisite project where the individual sites have different domains. We want to have a simple setup where both sites share an asset folder (web/cms-assets). So far, we're looking at the approach detailed here:

# .env
SITE_ONE_URL=https://site-one.com/
SITE_TWO_URL=https://site-two.com/
// config/general.php
'aliases' => [
    '@siteOneUrl' => getenv('SITE_ONE_URL'),
    '@siteTwoUrl' => getenv('SITE_TWO_URL'),
],

Then use those aliases for the baseUrl setting for the two sites. Now the problem is that now the @web alias is not defined, so Craft infers it from the request. The docs are pretty clear that this is not recommmended. So we would need a way to define the @web alias based on the current site's URL. One approach would be to check the current URL and set the alias to the matching domain based on that in config/general.php – but that feels wrong, and it doesn't work for console requests. Another problem are asset URLs – currently, the asset volume's base URL is set to @web/cms-assets/. We'd like for this to resolve to an absolute URL with the URL of the current site, but this means settings the @web alias based on the current URL again.

Is there a way to set the @web alias based on the current site's baseUrl? Or are we approaching this in the wrong way? What would be the standard/recommended way to approach this?

4
  • 1
    Maybe I'm missing something, what's wrong with setting @web to an empty string in general.php? I'm not using it for anything. Asset paths can start with @siteOneUrl/@siteTwoUrl if you want absolute URLs, but often I just use a symlink instead so that I can use site-root relative paths. Commented Jul 22, 2022 at 15:27
  • @JamesSmith Currently we have @web set to /, so everything using it will use absolute paths relative to the current URL. This should work for the asset URLs at least. IIRC some console commands require an URL for the @web though, or am I misremembering something?
    – MoritzLost
    Commented Jul 22, 2022 at 15:38
  • 1
    I have a note to myself in a multi site project that asset (re)indexing via the console requires a full base path (the filesystem path) to work properly, but not necessarily a full URL. So I also abstracted the base path to env vars. Commented Jul 22, 2022 at 17:36
  • @JamesSmith Thanks for the insights, I'll test that out next week!
    – MoritzLost
    Commented Jul 24, 2022 at 11:20

0

Your Answer

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

Browse other questions tagged or ask your own question.