Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 823
John Avatar asked Nov 29 '25 01:11

John


2 Answers

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" />
like image 118
Philip Avatar answered Nov 30 '25 15:11

Philip


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/';
like image 36
craig Avatar answered Nov 30 '25 13:11

craig



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!