codeigniter Setting your base url in Codeigniter


Example

You will need to set your base URL in application/config/config.php

If it is not set, then CodeIgniter will try to guess the protocol and path to your installation, but due to the security concerns the hostname will be set to $_SERVER['SERVER_ADDR'] if available, or localhost otherwise. The auto-detection mechanism exists only for convenience during development and MUST NOT be used in production!

$config['base_url'] = '';

It should be filed like

$config['base_url'] = 'http://localhost/projectname/';

$config['base_url'] = 'http://www.example.com/';

Always good to use / at end of base_url

When you do not set your base URL you might run into some errors where you can not load your CSS, images, and other assets items. And also you might have trouble submitting forms as some users have come across.

Update

If you do not want to set your base URL another way is.

Create a new core file in application/core/MY_Config.php

And paste this code

<?php

class MY_Config extends CI_Config {

    public function __construct() {

        $this->config =& get_config();

        log_message('debug', "Config Class Initialized");

        // Set the base_url automatically if none was provided

        if ($this->config['base_url'] == '')
        {
            if (isset($_SERVER['HTTP_HOST']))
            {
                $base_url = isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off' ? 'https' : 'http';
                $base_url .= '://'. $_SERVER['HTTP_HOST'];
                $base_url .= str_replace(basename($_SERVER['SCRIPT_NAME']), '', $_SERVER['SCRIPT_NAME']);
            }

            else
            {
                $base_url = 'http://localhost/';
            }

            $this->set_item('base_url', $base_url);

        }
    }
}