Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-14T18:53:35.537Z
Answers.Text: 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
Answers.UserDisplayName: Trevor Davis
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-14T19:00:51.060Z
Answers.Text: 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.
Answers.UserDisplayName: Brandon Kelly
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-11-26T19:53:06.370Z
Answers.Text: What value should be entered in the CP General Settings `Site URL` field when using the `siteUrl` config setting?
Answers.UserDisplayName: Marion Newlevant
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-11-27T04:17:31.777Z
Answers.Text: @MarionNewlevant if the siteUrl config setting is set, the CP setting is irrelevant.
Answers.UserDisplayName: Brandon Kelly
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-07-01T14:34:42.763Z
Answers.Text: 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
Answers.UserDisplayName: mjr
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-10-07T15:11:22.840Z
Answers.Text: Should the siteUrl in the array always be http, rather than https?
Answers.UserDisplayName: buck1112
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2014-07-10T21:12:15.417Z
Answers.LastActivityDate: 2019-03-14T08:34:02.323Z
Answers.LastEditDate: 2019-03-14T08:34:02.323Z
Answers.UserDisplayName: Brandon Kelly
Body: <p>There are multiple ways to set the site URL. What is the recommended way?</p>
  
Tags: <url><best-practice>
Title: What’s the recommended way to set the site URL?
ThreadText: # 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](https://docs.craftcms.com/v2/config-settings.html#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`](http://buildwithcraft.com/docs/php-constants#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](http://buildwithcraft.com/docs/multi-environment-configs)**. (Yes you can achieve this using the [`environmentVariables`](http://buildwithcraft.com/docs/config-settings#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**: @username_2 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?  
  
  ---
  
  