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.

I'm building www.mynewcraftsite.com.au, but I also wanna create www.mynewcraftsite.dev which is a testing server to preview to my clients. I've heard that I can set this up with Craft without going through the drudgery of downloading/uploading databases and updating urls in the db, users ... etc.

I've been reading the Craft CMS documentation for "Multi-Environment Configs". So far I've got this in my db.php file;

return array(
    //All environments
    '*' => array(
        'omitScriptNameInUrls' => true,
        'localhost' or '127.0.0.1'.
        'server' => 'localhost',
        'user' => 'dashing-print',
        'password' => 'f#$njEwFRn3&2',
        'database' => 'dashing-print',
        'tablePrefix' => 'craft'
    ),

    //Testing environment
    'www.mynewcraftsite.dev' => array(
        'siteUrl' => 'www.dashingprint.dev'
    ),

    //Live environment
    'www.mynewcraftsite.com.au' => array(
        'siteUrl' => 'www.dashingprint.com.au'
    )
);

It works EXCEPT when I go to www.mynewcraftsite.dev and then click on the links, I end up back on www.mynewcraftsite.com.au (eg. Instead of going to www.mynewcraftsite.dev/services, I go to www.mynewcraftsite.com.au/services. In order to get to the correct page I gotta write up the whole url www.mynewcraftsite.dev/service into the google chrome url bar.)

The siteURL property isn't doing anything, there's gotta be something else that I gotta stick in there in order to rewrite the urls.

share|improve this question

2 Answers 2

Make sure to change the "Site URL" setting to {siteUrl} in your control panel settings...

Control Panel > Settings > General > "Site URL"

Within your config file, you'll also want to put your siteUrl value inside of an Environment-Specific Variables sub-array:

'mynewcraftsite.dev' => array(
    'devMode' => true,
    'environmentVariables' => array(
        'siteUrl' => 'http://www.mynewcraftsite.dev/'
    )
),
'mynewcraftsite.com.au' => array(
    'environmentVariables' => array(
        'siteUrl' => 'http://www.mynewcraftsite.com.au/'
    )
)

Here's how Craft is compiling all of that...

Within each of your environment configurations, there are many config settings that you can apply to that specific environment. Additionally, each environment can have a sub-array called environmentVariables, which allows you to set whatever unique variables you want. The most common examples are siteUrl and fileSystemPath (great for setting up assets sources), but you can truly set whatever environment variables you want.

Once you've set the siteUrl value in your environment variables array, you now need to use that value... This is where the control panel settings page comes into play. Changing "Site URL" to simply {siteUrl} tells Craft to pull that value from your environment variables.

IMPORTANT: Notice the single set of curly braces, and the lack of spacing between the variable name and the braces. This is the syntax difference between environment variables and Twig variables.

That's it! When you create a path, Craft will use whatever is in the "Site URL" setting. And since that's pulling in your environment variable, that's what you'll get.


Lastly, allow me to point out a few things in your example which may also be stopping this from working for you (feel free to update the example in your question to fix these issues)...

Most importantly: You're combining data that should be stored in two separate files... craft/config/db.php and craft/config/general.php. While the multi-environment format is the same, you'll need to separate the data shown in your example. Put most of your settings into general.php, only the following belong in db.php:

  • server
  • user
  • password
  • database
  • tablePrefix

You also have an errant line of code in your example... This line should definitely be removed:

'localhost' or '127.0.0.1'.

Finally, you've left some crucial security information in your example... All of your database connection info is exposed (as well as your true domain names in your siteUrl examples).


Hope that helps... If you fix the bugs in your example code, I'll remove my corresponding notes from this answer. :)

share|improve this answer

The siteUrl value you are trying to set should not be set in the db.php file. It needs to be set in the general.php file. It also seems like you have a few other variables that should be moved over to general.php too.

You have one line that needs to be removed completely or commented out. 'localhost' or '127.0.0.1'. is not correct.

general.php

return array(
    //All environments
    '*' => array(
        'omitScriptNameInUrls' => true,
    )

    //Testing environment
    'www.mynewcraftsite.dev' => array(
        'environmentVariables' => array(
            'siteUrl' => 'http://www.mynewcraftsite.dev'
        )
    ),

    //Live environment
    'www.mynewcraftsite.com.au' => array(
        'environmentVariables' => array(
            'siteUrl' => 'http://www.mynewcraftsite.com.au'
        )
    )
);

db.php

return array(

    //All environments
    '*' => array(
        'tablePrefix' => 'craft'
    ),

    //Testing environment
    'www.mynewcraftsite.dev' => array(
        'server' => 'localhost',
        'user' => 'testuser',
        'password' => 'testpassword',
        'database' => 'testdatabase'
    ),

    //Live environment
    'www.mynewcraftsite.com.au' => array(
        'server' => 'localhost',
        'user' => 'liveuser',
        'password' => 'livepassword',
        'database' => 'livedatabase'
    )
);
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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