1

I have a craft 4 multi site, each site has its own domain. In the cp settings for each site I have entered each domain. This works fine for the production environment but is obviously useless when working locally. How can I set up my urls like so:

  • Production: each site is accessible via it's domain
  • Local: each site is accessible to me like this: https://mylocaldomain/site-one

I've read through the Craft Docs but I wasn't able to figure it out (I'm more of a designer than a developer!)

2 Answers 2

2

Sites' "Base URL" setting can be set to an environment variable, in the format $PRIMARY_SITE_URL, $SWEDISH_SITE_URL etc. (note the dollar sign in front, which signals to Craft that the value stored should be parsed as an environment variable and not used verbatim):

Using an environment variable for a site base URL

Essentially, this is an effective way of making a value that is otherwise persistent across environments (the raw site base URL values are stored in the project config), still possible to tweak on a per-environment level in a safe manner.

The environment variables can be named anything.

For local dev, most people will define the actual variables in a gitignored .env file that lives in the project's root folder, e.g.

PRIMARY_SITE_URL=https://example.com
SWEDISH_SITE_URL=https://example.se

Here are some directions on how to define environment variables with DDEV, specifically.

In production, you can still use an .env file, or set the variables at the server level through whatever method of your choice.

More info in the official docs on environmental configuration can be found here and here.

PS: When you set the site base URL setting to an environment variable, make sure that this variable actually exists in the current environment first. There's a reported bug/gotcha where, if the environment variable does not exist, Craft will actually save the base URL value with an http:// in front of the environment variable, to the project config.

1

I suggest you to create a custom domain in you local pc, by add it into your hosts file. Usually the path for this file is on /etc/hosts, or in c:\Windows\System32\Drivers\etc\hosts for windows. So you can add it like this

127.0.0.1   domain-one.test
127.0.0.1   domain-two.co 

And in you can use that url in your craft settings.

2
  • I'm using DDEV and Docker so I'm not sure that will work for me. Plus I still wouldn't know what to put in the site domain settings in the craft cp so that the user is taken to the correct multisite on the production version. Isn't there an environmental variable I can add to the site url settings in the cp, the value of which will change depending on the environment (I'm guessing I would need to add some code to the general config file specifying this)? Commented Jan 17, 2023 at 16:47
  • you can put the url on env variable, and that also hosts file should be applicable even you using docker, btw i think the answer from Mats Mikkel Rummelhoff is the answer how to setting the environment variable.
    – aodihis
    Commented Jan 18, 2023 at 2:36

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.