CodeIgniter base_url(), link in localhost gives me to live server
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/';
John
Updated on July 09, 2022Comments
-
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 about 9 yearswhat with file application/config/config.php should i delete it ?
-
Philip about 9 yearsYou can yes, once you have a copy of it in both
development
andproduction
folders. You can do the same withdatabase.php
-
John about 9 yearsOkey I did what you say but my link show to non existing link it looks like for example localhost/assets/style.css .
-
Philip about 9 yearsYou 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 sureENV=='development'
insideindex.php
-
John about 9 yearsIn 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 about 9 yearslinks 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 about 9 yearsLocalhost should match where your project folder is. localhost/your_project or leave base_url empty and codeigniter will guess it
-
John about 9 yearsI 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 about 9 yearsI 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 about 9 yearsyes 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 about 9 yearsfinally, problem was in CI in .htaccess with RewriteBase I set it off
-
uneeb meer over 6 yearsthe 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..:)