There are multiple ways to set the site URL. What is the recommended way?
1 Answers
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/',
),
// ...
),
// ...
);
- 34,307
- 2
- 71
- 137
{siteUrl}tag in your asset source settings, you will still need to define a'siteUrl'key in your environmentVariables array. That may change in the future though, since it would be handy. – Brandon Kelly Jul 14 '14 at 19:00Site URLfield when using thesiteUrlconfig setting? – Marion Newlevant Nov 26 '14 at 19:53