How to set proper codeigniter base url?
Base URL should be absolute, including the protocol:
$config['base_url'] = "http://somesite.com/somedir/";
If using the URL helper, then base_url()
will output the above string.
Passing arguments to base_url()
or site_url()
will result in the following (assuming $config['index_page'] = "index.php";
:
echo base_url('assets/stylesheet.css'); // http://somesite.com/somedir/assets/stylesheet.css
echo site_url('mycontroller/mymethod'); // http://somesite.com/somedir/index.php/mycontroller/mymethod
Set up the base url in codeigniter
In Config.php
$config['base_url'] = 'http://localhost/Appsite/website/';
$config['index_page'] = '';
# If online site
# $config['base_url'] = 'http://stackoverflow.com/';
In .htaccess
(outside application folder) - To remove index.php
in URL
RewriteEngine on
RewriteCond $1 !^(index\.php|assets|image|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
To accessing URL
<a href="<?php echo base_url();?>contollerName/methodName"> click here</a>
To access image
<img src="<?php echo base_url();?>images/images.PNG”>
To access CSS
<link rel="stylesheet" type="text/css" href="<?php echo base_url();?>assets/css/style.css"/>
To use base_url
load URL helper from autoload.php
How to set config_base url in config.php file in codeigniter?
You should got to this file path
Codeigniter/applications/config/config.php
Search for this line that contains
$config['base_url'] = ""
Then your base_url should be
$config['base_url'] = "http://yoursite.com/"
If xampp, it should be
$config['base_url'] = "http://localhost/yoursite/"
And if wamp, it should be
$config['base_url'] = "http://www/yoursite/"
Note that yoursite is the filename of the site folder you have in htdocs or www folder
When you make use of <?php echo base_url(directory/file_name); ?>
In your views, it should output something like:
"http://localhost/yoursite.com/directory/file_name"
That should do the trick
Setting BaseURL for Codeigniter
Ok, well for anyone wondering the final solution to this was that the directories were not properly named. When creating a local server it is important to put the contents of the files in the correct folder.
So if you have http://localhost/codeigniter
then the directory should be setup as \path\to\webdirectory\root\codeigniter
or in this case: E:\WEBD\xampp\htdocs\codeigniter
base url for code igniter site not working
You are not using the base_url
in your HTML code, you are currently "manually" loading the assets:
<link href="/assets/css/global.css" rel="stylesheet" type="text/css" media="all" />
The /
at the start tells the browser to load the assets from the root folder.
You can use the base_url()
function to generate links, this function is available if you load the URL helper, you can use it like this:
base_url('assets/css/global.css');
This will return:
http://xxx.xxx.xx.xxx/blah.com/assets/css/global.css
So use it like this:
<link href="<?=base_url('assets/css/global.css');?>" rel="stylesheet" type="text/css" media="all" />
<?=
is the same as <?php echo
You can load the URL helper with this code:
$this->load->helper('url');
Related Topics
What's the Difference Between Isset() and Array_Key_Exists()
Streaming a Large File Using PHP
How to Write a PHP Ternary Operator
How to Stop People Hacking the PHP-Based Highscore Table of a Flash Game
Getting the Names of All Files in a Directory With PHP
How to Start and End Transaction in MySQLi
Can Png Image Transparency Be Preserved When Using PHP'S Gdlib Imagecopyresampled
PHP: Regex to Ignore Escaped Quotes Within Quotes
Are Arrays in PHP Copied as Value or as Reference to New Variables, and When Passed to Functions
Force File Download With PHP Using Header()
How to Fix Error: Laravel.Log Could Not Be Opened
Running MySQL *.SQL Files in PHP
How to Get Multiple Parameters With Same Name from a Url in PHP