As I have read in some resources, base_url() and site_url() functions in codeigniter are almost the same, although my version of code
Or, you can create file .htaccess in the CodeIgniter root folder next to its index.php file.
just add this code on .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
this code will make the word index.php remain unseen.
http://localhost/CodeIgniter/index.php/controllersName/
will be changed to this
http://localhost/CodeIgniter/controllersName/
base_url() that is commonly used in Codeigniter can be used even without the .php extension.
site_url() which is commonly used in Wordpress custom template can be used even when you just call the post_title of the blog or the file name with the extension.
site_url() is execute index.php that why site_url is better as compare to base_url.
The purpose of site_url is that your pages become more portable in the event your URL changes.The site_url appears with the index.php file.
Segments can be optionally passed to the function as a string or an array.
echo site_url("news/local/123");
it will give: http://ci.com/index.php/news/local/123
you can even pass segments as an array:
$segments = array('news', 'local', '123');
echo site_url($segments);
base_url is without the index_page or url_suffix being appended.
like site_url, you can supply segments as a string or an array.
If you want a URL access to a resource use base_url you can supply a string to a file, such as an image or stylesheet else site_url is enough.
echo base_url("/images/icons/image.png");
I would like to add when to use base_url() and the site_url() . Basically one can use site_url() while creating links for controllers whereas base_url() can be used where we need to create urls for the assets like loading a css or js file or some image .
What I always prefer is to use site_url() for creating links to controllers or ajax urls and base_url() for loading assets .
echo base_url(); // http://example.com/website
echo site_url(); // http://example.com/website/index.php
if you want a URL access to a resource (such as css, js, image), use base_url(), otherwise, site_url() is better.
for a detailed reference Check this both function in CodeIgniter.
public function site_url($uri = '')
{
if (empty($uri))
{
return $this->slash_item('base_url').$this->item('index_page');
}
$uri = $this->_uri_string($uri);
if ($this->item('enable_query_strings') === FALSE)
{
$suffix = isset($this->config['url_suffix']) ? $this->config['url_suffix'] : '';
if ($suffix !== '')
{
if (($offset = strpos($uri, '?')) !== FALSE)
{
$uri = substr($uri, 0, $offset).$suffix.substr($uri, $offset);
}
else
{
$uri .= $suffix;
}
}
return $this->slash_item('base_url').$this->slash_item('index_page').$uri;
}
elseif (strpos($uri, '?') === FALSE)
{
$uri = '?'.$uri;
}
return $this->slash_item('base_url').$this->item('index_page').$uri;
}
Base URL function.
public function base_url($uri = '')
{
return $this->slash_item('base_url').ltrim($this->_uri_string($uri), '/');
}