<p>Sites' &quot;Base URL&quot; setting can be set to an <a href="https://craftcms.com/docs/4.x/config/#env" rel="nofollow noreferrer">environment variable</a>, in the format <code>$PRIMARY_SITE_URL</code>, <code>$SWEDISH_SITE_URL</code> 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):</p>
<p><a href="https://i.sstatic.net/lkqLL.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/lkqLL.png" alt="Using an environment variable for a site base URL" /></a></p>
<p>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 <a href="https://craftcms.com/docs/4.x/project-config.html#scope" rel="nofollow noreferrer">project config</a>), still possible to tweak on a per-environment level in a safe manner.</p>
<p>The environment variables can be named anything.</p>
<p><strong>For local dev</strong>, most people will define the actual variables in a gitignored <code>.env</code> file that lives in the project's root folder, e.g.</p>
<pre><code>PRIMARY_SITE_URL=https://example.com
SWEDISH_SITE_URL=https://example.se
</code></pre>
<p>Here are some <a href="https://ddev.readthedocs.io/en/latest/users/extend/customization-extendibility/#providing-custom-environment-variables-to-a-container" rel="nofollow noreferrer">directions on how to define environment variables with DDEV</a>, specifically.</p>
<p><strong>In production</strong>, you can still use an <code>.env</code> file, or set the variables at the server level through whatever method of your choice.</p>
<p>More info in the official docs on environmental configuration can be found <a href="https://craftcms.com/docs/4.x/project-config.html#secrets-and-the-environment" rel="nofollow noreferrer">here</a> and <a href="https://craftcms.com/docs/4.x/config/#control-panel-settings" rel="nofollow noreferrer">here</a>.</p>
<p>PS: When you set the site base URL setting to an environment variable, <em>make sure that this variable actually exists in the current environment first</em>. There's a <a href="https://github.com/craftcms/cms/issues/6528" rel="nofollow noreferrer">reported bug/gotcha</a> where, if the environment variable does <em>not</em> exist, Craft will actually save the base URL value with an <code>http://</code> in front of the environment variable, to the project config.</p>

CC BY-SA 4.0
2023-01-17T22:48:48.673Z
2023-01-17T23:11:43.107Z
2023-01-17T23:11:43.107Z
Mats Mikkel Rummelhoff
<p>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</p>
<pre><code>127.0.0.1   domain-one.test
127.0.0.1   domain-two.co 
</code></pre>
<p>And in you can use that url in your craft settings.</p>

CC BY-SA 4.0
2023-01-17T16:47:26.937Z
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)?
George Bleasdale
CC BY-SA 4.0
2023-01-18T02:36:30.833Z
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
CC BY-SA 4.0
2023-01-17T08:17:57.547Z
2023-01-17T08:17:57.547Z
aodihis
<p>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:</p>
<ul>
<li>Production: each site is accessible via it's domain</li>
<li>Local: each site is accessible to me like this: https://mylocaldomain/site-one</li>
</ul>
<p>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!)</p>

<multi-site><multi-environment><craft4><multi-domain>
Defining a domain for each site for both production and local - craft 4 multisite
# Defining a domain for each site for both production and local - craft 4 multisite

(Asked by: username_0 on 2023-01-17)

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!)

**Tags:** multi-site, multi-environment, craft4, multi-domain


---

## 2 answers

### Answer by username_1 on 2023-01-17. Score: 2 | Accepted answer

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):

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.


---

### Answer by username_2 on 2023-01-17. Score: 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.

> **Comments:**
> **username_0**: 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)?  
> **username_2**: 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 username_1  is the answer how to setting the environment variable.  

---

