<p>As of Craft 2, <strong>you should be using the new <a href="https://docs.craftcms.com/v2/config-settings.html#siteurl" rel="nofollow noreferrer">siteUrl</a> config setting</strong>.</p>

<hr>

<p>There are two advantages to setting your site URL using this config setting.</p>

<p>First, The value can be set on a <strong>per-locale basis</strong>, in a way where Craft is actually aware of all locales’ URLs on every request (unlike the <a href="http://buildwithcraft.com/docs/php-constants#craft-site-url" rel="nofollow noreferrer"><code>CRAFT_SITE_URL</code></a> constant, where Craft only knows the current locale’s URL). That’s a good thing because it means you can easily link to entries’ URLs across different locales, for example.</p>

<pre><code>return array(

    'siteUrl' =&gt; array(
        'en_us' =&gt; 'http://example.com/',
        'de'    =&gt; 'http://example.com/de/',
    ),

    // ...

);
</code></pre>

<p>Its value can also be set directly on a <strong><a href="http://buildwithcraft.com/docs/multi-environment-configs" rel="nofollow noreferrer">per-environment basis</a></strong>. (Yes you can achieve this using the <a href="http://buildwithcraft.com/docs/config-settings#environmentVariables" rel="nofollow noreferrer"><code>environmentVariables</code></a> config setting combined with a <code>{siteUrl}</code> tag inside the Site URL setting within the CP’s general settings, but that’s much less direct/obvious.)</p>

<pre><code>return array(

    '.dev' =&gt; array(
        'siteUrl' =&gt; 'http://example.dev/',
        // ...
    ),

    '.com' =&gt; array(
        'siteUrl' =&gt; 'http://example.com/',
        // ...
    ),

    // ...
);
</code></pre>

<p>Those of course can be combined as well:</p>

<pre><code>return array(

    '.dev' =&gt; array(
        'siteUrl' =&gt; array(
            'en_us' =&gt; 'http://example.dev/',
            'de'    =&gt; 'http://example.dev/de/',
        ),
        // ...
    ),

    '.com' =&gt; array(
        'siteUrl' =&gt; array(
            'en_us' =&gt; 'http://example.com/',
            'de'    =&gt; 'http://example.com/de/',
        ),
        // ...
    ),

    // ...

);
</code></pre>

CC BY-SA 3.0
2014-07-14T18:53:35.537Z
I just switched over to setting siteUrl this way, but it seems like the variable doesn't get parsed anymore in the asset sources? Here is what my config looks like: https://gist.github.com/davist11/aea165a093b20e159782
Trevor Davis
CC BY-SA 3.0
2014-07-14T19:00:51.060Z
If you want to use a `{siteUrl}` tag in your asset source settings, you will still need to define a `'siteUrl'` key in your [environmentVariables](http://buildwithcraft.com/docs/config-settings#environmentVariables) array. That may change in the future though, since it would be handy.
Brandon Kelly
CC BY-SA 3.0
2014-11-26T19:53:06.370Z
What value should be entered in the CP General Settings `Site URL` field when using the `siteUrl` config setting?
Marion Newlevant
CC BY-SA 3.0
2014-11-27T04:17:31.777Z
@MarionNewlevant if the siteUrl config setting is set, the CP setting is irrelevant.
Brandon Kelly
CC BY-SA 3.0
2016-07-01T14:34:42.763Z
It's worth noting that for security reasons you should not set siteUrl dynamically with HTTP_HOST https://ellislab.com/blog/entry/http-host-and-server-name-security-issues
mjr
CC BY-SA 4.0
2024-10-07T15:11:22.840Z
Should the siteUrl in the array always be http, rather than https?
buck1112
CC BY-SA 4.0
2014-07-10T21:12:15.417Z
2019-03-14T08:34:02.323Z
2019-03-14T08:34:02.323Z
Brandon Kelly
<p>There are multiple ways to set the site URL. What is the recommended way?</p>

<url><best-practice>
What’s the recommended way to set the site URL?
# What’s the recommended way to set the site URL?

(Asked by: username_0 on 2014-07-10)

There are multiple ways to set the site URL. What is the recommended way?

**Tags:** url, best-practice

---

## 1 answers

### Answer by username_0 on 2014-07-10. Score: 48 | Accepted answer

As of Craft 2, you should be using the new siteUrl config setting.

There are two advantages to setting your site URL using this config setting.
First, The value can be set on a per-locale basis, in a way where Craft is actually aware of all locales’ URLs on every request (unlike the CRAFT_SITE_URL constant, where Craft only knows the current locale’s URL). That’s a good thing because it means you can easily link to entries’ URLs across different locales, for example.
return array(

    'siteUrl' => array(
        'en_us' => 'http://example.com/',
        'de'    => 'http://example.com/de/',
    ),

    // ...

);

Its value can also be set directly on a per-environment basis. (Yes you can achieve this using the environmentVariables config setting combined with a {siteUrl} tag inside the Site URL setting within the CP’s general settings, but that’s much less direct/obvious.)
return array(

    '.dev' => array(
        'siteUrl' => 'http://example.dev/',
        // ...
    ),

    '.com' => array(
        'siteUrl' => 'http://example.com/',
        // ...
    ),

    // ...
);

Those of course can be combined as well:
return array(

    '.dev' => array(
        'siteUrl' => array(
            'en_us' => 'http://example.dev/',
            'de'    => 'http://example.dev/de/',
        ),
        // ...
    ),

    '.com' => array(
        'siteUrl' => array(
            'en_us' => 'http://example.com/',
            'de'    => 'http://example.com/de/',
        ),
        // ...
    ),

    // ...

);

> **Comments:**
> **username_1**: I just switched over to setting siteUrl this way, but it seems like the variable doesn't get parsed anymore in the asset sources? Here is what my config looks like: https://gist.github.com/davist11/aea165a093b20e159782  
> **username_0**: If you want to use a `{siteUrl}` tag in your asset source settings, you will still need to define a `'siteUrl'` key in your [environmentVariables](http://buildwithcraft.com/docs/config-settings#environmentVariables) array. That may change in the future though, since it would be handy.  
> **username_2**: What value should be entered in the CP General Settings `Site URL` field when using the `siteUrl` config setting?  
> **username_0**: @MarionNewlevant if the siteUrl config setting is set, the CP setting is irrelevant.  
> **username_3**: It's worth noting that for security reasons you should not set siteUrl dynamically with HTTP_HOST https://ellislab.com/blog/entry/http-host-and-server-name-security-issues  
> **username_4**: Should the siteUrl in the array always be http, rather than https?  

---

