Take the 2-minute tour ×
Craft CMS Stack Exchange is a question and answer site for administrators, end users, developers and designers for Craft CMS. It's 100% free, no registration required.

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

share|improve this question

1 Answer 1

up vote 23 down vote accepted

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/',
        ),
        // ...
    ),

    // ...

);
share|improve this answer
    
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: gist.github.com/davist11/aea165a093b20e159782 –  Trevor Davis Jul 14 at 18:53
4  
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 array. That may change in the future though, since it would be handy. –  Brandon Kelly Jul 14 at 19:00
    
Got it, thanks! –  Trevor Davis Jul 14 at 19:01

protected by Victor In Jul 29 at 18:22

Thank you for your interest in this question. Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.