CodeIgniter base_url(), link in localhost gives me to live server CodeIgniter base_url(), link in localhost gives me to live server codeigniter codeigniter

CodeIgniter base_url(), link in localhost gives me to live server


You can create two folders inside application/config one for production(live server) and one for development(localhost)

  • development => application/config/development
  • production => application/config/production

Then copy any file in the main application/config folder and paste it into both development and production folders.

application/config/development/config.php

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

application/config/production/config.php

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

Based on your ENV constant in index.php it will load settings from either production or development folders. So if ENV == 'development' the settings in application/config/development will get used.

If you want a relative path to your assets, add this to the HEAD of your html

<base href="<?php echo base_url();?>" />

Then you can just ref your assets like so

<link rel="stylesheet" href="assets/styles.css" />


You can set the below code to config.php and it will take relavent url if you are in localhost then it will take localhost and if you are in the live server then it will take live url.

$root=(isset($_SERVER['HTTPS']) ? "https://" : "http://").$_SERVER['HTTP_HOST'];$root.= str_replace(basename($_SERVER['SCRIPT_NAME']), '', $_SERVER['SCRIPT_NAME']);$config['base_url'] = $root;//$config['base_url']   = 'http://localhost/abc/';


No need to touch config.php. Follow this solution, it will work both in local as well as in live, even after removing index.php through htaccsss

FOR adding files(css,images,js etc) use like following

  <?php echo base_url('assets/js/test.js');?>

and FOR rest of the stuff use following

   <?php echo site_url('controller/metho_name');?>

It will handle automatically in live or local server and keep your config.php as it is.

Thanks