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

33,796

Solution 1

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

Solution 2

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/';
Share:
33,796
John
Author by

John

Updated on July 09, 2022

Comments

  • John
    John almost 2 years

    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?

  • John
    John about 9 years
    what with file application/config/config.php should i delete it ?
  • Philip
    Philip about 9 years
    You can yes, once you have a copy of it in both development and production folders. You can do the same with database.php
  • John
    John about 9 years
    Okey I did what you say but my link show to non existing link it looks like for example localhost/assets/style.css .
  • Philip
    Philip about 9 years
    You need to set it to the correct url for localhost $config['base_url'] = 'http://localhost/<namespace>' OR you could leave it blank $config['base_url'] = '' If you are working locally, you need to make sure ENV=='development' inside index.php
  • John
    John about 9 years
    In index.php there is default development: define('ENVIRONMENT', 'development');. I also set it to the correct url $config['base_url'] = 'localhost/<namespace>' but the result is that only index page of the projekt is loading right.
  • John
    John about 9 years
    links in lives server are good but in localhost only in index page , it loads .css .js ... but when i click to link from index page it throw me to non existing link but url seems good
  • Philip
    Philip about 9 years
    Localhost should match where your project folder is. localhost/your_project or leave base_url empty and codeigniter will guess it
  • John
    John about 9 years
    I tried this for the first time, write path or leave base_url empty, the result was the same. Index.php is loading but nothing more. I will find the problem but when ....
  • Philip
    Philip about 9 years
    I don't really know what it could be, you say everything works ok on index.php yet all views are run through index.php. My solution should work.
  • John
    John about 9 years
    yes your solution works excellent and it helps me but my problem will be maybe somewhere in controller where url is viewed ... when i try call something with ending .php .jpg .css .... it works fine but when i call something without ending like <a href=" <?= base_url() ?>show/edit/car"> , there is problem error 404 . But this problem is only in localhost
  • John
    John about 9 years
    finally, problem was in CI in .htaccess with RewriteBase I set it off
  • uneeb meer
    uneeb meer over 6 years
    the best way to define base url i was facing an issue for a long time only this solution resolved my issues i wish i could give you 100 upvotes..:)