Automatic configuration of URL in CodeIgniter

In this mini-post we are going to explain how to configure our CodeIgniter in order to make it run on any server without having to be modifying the parameter “base_url” in the configuration. So, we can move our development version of our site to another different server (let’s say, the production server), without worrying about this parameter. In order to make the parameter “base_url” automatic, all we have to do is to modify the following code in the file application/config/config.php:

$config['base_url'] = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == "on") ? "https" : "http");
$config['base_url'] .= "://".$_SERVER['HTTP_HOST'];
if (!isset($_SERVER['ORIG_SCRIPT_NAME']))
{
  $config['base_url'] .= str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']);
}
else
{
  $config['base_url'] .= str_replace(basename($_SERVER['ORIG_SCRIPT_NAME']),"",$_SERVER['ORIG_SCRIPT_NAME']);
}

And that’s all! With this simple code we can move our CodeIgniter from one server to another and we will not need to change this parameter in the configuration.

Leave a Reply

Your email address will not be published. Required fields are marked *