2

I followed the advice from this and this post, but I just can't get my environment specific siteUrls to work.

This is what my config file looks like:

return array(

    '*' => array(
      'errorTemplatePrefix' => "pages/",
    ),

    '.dev' => array(
      'environmentVariables' => array(
        'userSessionDuration' => false,
        'devMode' => true,
        'siteUrl ' => 'http://myurl.dev',
      )
    ),

    '.com' => array(
      'environmentVariables' => array(
        'userSessionDuration' => false,
        'devMode' => true,
        'siteUrl ' => 'http://myurl.com',
      )
    ),

    '.org' => array(
      'environmentVariables' => array(
        'siteUrl'  => 'http://myurl.org',
      )
    )
);

These do not take precedence over any URL specified in Settings > General in the CP as outlined in the docs. If I set the Site URL field to {siteUrl} in the CP, then my links are broken and look like this: {siteUrl}/link-to-a-page.

Am I missing something else to get the siteUrl settings to work?


Update

Fixed the additional space in 'siteUrl ', thanks to Lindsey.

My config settings now look like this:

'*' => array(
  'errorTemplatePrefix' => "pages/",
),
// local
'.dev' => array(
  'devMode' => true,
  'userSessionDuration' => false,
  'environmentVariables' => array(
    'siteUrl' => 'http://myurl.dev',
  )
),
// staging
'.com' => array(
  'devMode' => true,
  'userSessionDuration' => false,
  'environmentVariables' => array(
    'siteUrl' => 'http://myurl.com',
  )
),
// live
'.org' => array(
  'environmentVariables' => array(
    'siteUrl'  => 'http://myurl.org',
  )
)  

With this and adding {siteUrl} in the CP, everything works as expected - hooray!

It does however not reflect what the Craft docs say, as they state that the config settings "will take precedence over the Site URL setting in Settings → General". I've contacted the Craft team to let them know about this.


Update #2

Got this response from Brandon:

“I'm guessing that when you added 'siteUrl' to your config, you put it inside the environmentVariables array? (which would explain why adding {siteUrl} to the CP setting made it work.)

When the documentation is referring to the siteUrl config setting, it's referring to an actual top-level config setting, not some custom "siteUrl" environmentVariable that happens to have the same name.”

So I've taken the setting out of environmentVariables, and hey presto, it all works as expected. You even get a helpful message in the CP:

screenshot

Final working code:

'*' => array(
  'errorTemplatePrefix' => "pages/",
),
// local
'.dev' => array(
  'devMode' => true,
  'userSessionDuration' => false,
  'siteUrl' => 'http://myurl.dev',
),
// staging
'.com' => array(
  'devMode' => true,
  'userSessionDuration' => false,
  'siteUrl' => 'http://myurl.com',
),
// live
'.org' => array(
  'siteUrl'  => 'http://myurl.org',
)

Got there in the end - phew!

3

Make sure that you change .dev, .com, etc. to reflect your actual environment URLs. They operate based on a partial string match, so as long as your URL contains one of those substrings, it'll pick up the first one it matches.

For example:

'local.mywebsite' => array(
  'environmentVariables' => array(
    'userSessionDuration' => false,
    'devMode' => true,
    'siteUrl ' => 'http://local.mywebsite',
  )
),

'mywebsite.com' => array(
  'environmentVariables' => array(
    'userSessionDuration' => false,
    'devMode' => true,
    'siteUrl ' => 'http://mywebsite.com',
  )
),

UPDATE

Whoa, I think I may have just realized what the problem is... I copied my example from your original code, and copied a glitch right along with it...

You've listed siteUrl like this:

    'siteUrl ' => 'http://mywebsite.com'

Notice anything? There is an extra space between siteUrl and the closing quote.

    'siteUrl '

... should be:

    'siteUrl'
  • Thank you Lindsey with the eagle eyes! Words cannot express how much of a n00b I feel right now... That’s got it working. However, the documentation states that the config settings take precedence over the Site URL setting in Settings → General, which is not the case. Whatever URL is put there is used, unless you put {siteUrl} in the field. I'll let the guys at Craft know about that. – Katrin Sep 17 '14 at 10:20
  • Oooooh. Easy mistake to make. Those key names could perhaps be trimmed when Craft is going over them, eh? – Fred Carlsen Sep 17 '14 at 10:45
2

As of Craft 2.2, you should now be using the new siteUrl setting. See this reply for more details.

Your variable siteUrl from environmentVariables should still work, though.

What do you see if you add this to a test template?

<pre>
  {{ dump(craft.config.get('environmentVariables')) }}
</pre>

Check out How is the Site URL determined? from the docs as well.

  • I get this: array(1) { ["siteUrl "]=> string(30) "http://mysite.dev" } – Katrin Sep 16 '14 at 21:51
  • The post you link to was answered by Brandon on the 8th July. The first post I link to and followed was answered by Brandon on the 8th September, so technically what I've got should be working right? Hmpf. – Katrin Sep 16 '14 at 23:21
  • Well, environmentVariables in the Site URL setting works in my installations. What version are you on? You should send a support ticket through the support widget, with database-dump. Perhaps something fishy is going on. Either way, I suggest you go for the siteUrl config option instead, which is the recommended way. – Fred Carlsen Sep 16 '14 at 23:30
  • Thanks @fred, turns out I was being a n00b and need better glasses ;) – Katrin Sep 17 '14 at 10:39
  • We all need some strong glasses from time to time ;) – Fred Carlsen Sep 17 '14 at 10:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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