-
Notifications
You must be signed in to change notification settings - Fork 7.6k
Dynamic Base Url
Many times the base url in config/config.php must be set it to localhost for development and reset when it goes live. More importantly is when you are dealing with a single application accessible from multiple domains. So this provides the ability to easily set the value dynamically. I am not sure why I have neither come across anyone mentioning issues with it or its requirement considering what can be easily achieved. Never the less it is still very simple although you could add security measures since the input instance is not directly available in the config files since they don't reside in the superclass.
Just overwrite the line in config/config.php with the following:
$config['base_url'] = 'http://'.$_SERVER['HTTP_HOST'].'/';
EDIT:
WanWizard, as known on the forums has posted a much more thorough and flexible solution which takes apps ran in a subdirectory or through SSL (https) into account.
$config['base_url'] = (isset($_SERVER['HTTPS']) ? "https://" : "http://") . $_SERVER['HTTP_HOST'] . ROOT;
The constant ROOT is defined in index.php as:
define('ROOTPATH', realpath(dirname(__FILE__)) . '/');
// installed in the docroot?
if (realpath(dirname(__FILE__)) == $_SERVER['DOCUMENT_ROOT'])
{
define('ROOT', '/');
}
else
{
define('ROOT', substr(ROOTPATH, strlen($_SERVER['DOCUMENT_ROOT'])+1));
}