John
John

Reputation: 111

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?

Upvotes: 3

Views: 37580

Answers (3)

Rejoanul Alam
Rejoanul Alam

Reputation: 5398

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

Upvotes: 0

Philip
Philip

Reputation: 4592

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" />

Upvotes: 4

craig
craig

Reputation: 375

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

Upvotes: 3

Related Questions