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

John picture John · Apr 6, 2015 · Viewed 32.4k times · Source

I am working on web page in CodeIgniter in my localhost but this webpage is also in live server.

It uses base_url() for all links.

For example:

<link rel="stylesheet" href="<?= base_url() ?>assets/style.css">
<script src="<?= base_url() ?>assets/jquery-ui.min.js"></script>
...
<a href="<?= base_url() ?>show/edit/other">

When I click on link in localhost it gives mi live server address, so I can't do anything in localhost.

Can somebody help me please?

Answer

craig picture craig · Apr 6, 2015

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/';