Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set Dynamic Base Url in CodeIgniter

I need to set dynamic base url in codeigniter due to few following reasons.

  • localhost for development and reset when it goes live
  • single application accessible from multiple domains
  • ip address may change due to dhcp (locally)
like image 238
Janith Chinthana Avatar asked Aug 06 '13 08:08

Janith Chinthana


2 Answers

Also you can try this.

$base_url = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == "on") ? "https" : "http");
$base_url .= "://". @$_SERVER['HTTP_HOST'];
$base_url .=     str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']);
$config['base_url'] = $base_url;
like image 164
Rakhi Prajapati Avatar answered Oct 26 '22 11:10

Rakhi Prajapati


I just need to share my knowledge, since I already found the answer as mention below.

Just overwrite the line in config/config.php with the following:

$config['base_url']    = 'http://'.$_SERVER['HTTP_HOST'].'/';

If you are using the sub folder you can use following code:

$root = "http://".$_SERVER['HTTP_HOST'];
$root .= str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']);
$config['base_url']    = "$root";
like image 44
Janith Chinthana Avatar answered Oct 26 '22 12:10

Janith Chinthana