2

I was wondering if the following setup is a good practice.

In my .env file I defined a single rootUrl var which contains the site root url like this:

ROOT_URL="https://www.mydomain.test"

In the general.php config file i've created an alias (@rootUrl) that contains the ROOT_URL env var value:

'aliases' => [ '@rootUrl' => getenv('ROOT_URL'), ],

Then I define my craft sites as following (multilingual site), each site BASE URL is constructed with the rootUrl and a language:

Schermafbeelding 2019-03-22 om 08 39 32

Advantages

  • You don't have to define every site (language) in the code, languages are managed through the cms (in case of multisite setup, you should define a ROOT_URL for every site).
  • the @rootUrl alias can be used as part of the resource path in the templates.

Disadvantages

Does this logic makes sense or is it generally a bad idea to use aliases for the site BASE URL setting?

Thanks in advance

2

Good practice imo, it's what I do as well. Values come from the .env vars, and for things that are URLs or paths, I make aliases out of them to take advantage of what you're showing above.

This cannot cause any kind of issue like @web -- the only thing wrong with @web is that the host name is computed dynamically, potentially from untrusted browser data.

You're not doing that here, you are explicitly providing the host name, so it's totally fine. You could even do:

'aliases' => [
        '@web' => getenv('ROOT_URL'),
    ],

...to redefine the dynamically generated @web alias to something that is from a static value for the URL.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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