3

To get around a temporary issue where two urls go to the same site, I wanted to set all my site links to be relative rather than using the full url. Looking through the Craft docs I found that it's apparently possible to place something like this in the index.php file:

define('CRAFT_SITE_URL', "http://{host}/");

However this didn't seem to work - I just get '{host}' appearing in all my links. Instead I set the site url as below, which has had the desired effect...

define('CRAFT_SITE_URL', "/");

...but is this a bad idea?

1
  • Why not use a mod rewrite rule? Jul 13 '14 at 17:16
4

I may be mistaken, but I feel like a standard multi-environment configuration should work for you in this situation... specifically the use of environment-specific variables.

When you setup your multi-environment configuration, just set one up for each of your domain names. You would include something like this in your config/general.php file...

'my.first.domain' => array(
    'environmentVariables' => array(
        'siteUrl' => 'http://my.first.domain/'
    )
),
'my.second.domain' => array(
    'environmentVariables' => array(
        'siteUrl' => 'http://my.second.domain/'
    )
),

Once you've got that setup, login to your Control Panel and go to:

Settings > General

Change the value of Site URL to be "{siteUrl}".

Now, you can easily use any of these variations in your template:

  • {{ siteUrl }}/contact-us
  • url('contact-us')
  • siteUrl('contact-us')

It's a little tricky to explain the relationship between those three variations of "site url", but here's the gist of it...

  1. When you set an environment-specific variable in your config file, it can be parsed by Craft (in certain circumstances) with the syntax {siteUrl}. Note the single set of curly braces, and the lack of spaces between the variable name and the braces.

  2. The "Site URL" setting in your CP is capable of parsing out environment-specific variables. So when you put "{siteUrl}" in there, it represents whatever you put in your config file.

  3. Now, when you use {{ siteUrl }} in your templates, it uses whatever value is represented by the "Site URL" value in your CP. Since we just told that field to use the environment-specific variable, that's what will be parsed through to the front-end.

In my example, I also mentioned url() and siteUrl(). These two functions will automatically prepend the "Site URL" value to whatever string you specify.

The difference between the two only really matters if you're rendering a template in the Control Panel (using a plugin). url() will prepend the entire site url, including the /admin/ segment... meanwhile, siteUrl() will only prepend the base site url.

10
  • {siteUrl} with single curly braces (pt. 1) only work in the CP.
    – carlcs
    Jul 10 '14 at 21:15
  • @ChristianSeelbach They can also be parsed out via a plugin: craft()->config->parseEnvironmentString($myString)
    – Lindsey D
    Jul 10 '14 at 21:17
  • Possibly, but I think this isn't something @pumkincreative currently wants to do.
    – carlcs
    Jul 10 '14 at 21:21
  • Agreed, I probably explained way more than I needed to in my answer. To be fair, he was trying to use an environment variable in his original example.
    – Lindsey D
    Jul 10 '14 at 21:24
  • 1
    Brandon just posted this Q&A illustrating a different way to set the {{ siteUrl }} value... Perhaps that's a better method for @pumkincreative.
    – Lindsey D
    Jul 10 '14 at 21:26

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.