47

As I have read in some resources, base_url() and site_url() functions in Codeigniter are almost the same, although my version of Codeigniter (2.1.3) does not have a site_url() in its config.php file (in the config directory).

Yet are there differences between them in any way since I have seen site_url() with parameters and never seen base_url() holding none?

7 Answers 7

74
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), '/'); } 
Sign up to request clarification or add additional context in comments.

2 Comments

I guess @sikander's answer stackoverflow.com/a/17079780/248616 should clarify it better
You may view his answer in the given link in my comment - the bold difference about index.php should be highlighted.
23

site_url: Returns base_url + index_page + uri_string

base_url: Returns base_url + uri_string

See source code of both functions at: https://github.com/EllisLab/CodeIgniter/blob/606fee0e2e0aa6a906db82e77090e91f133d7378/system/core/Config.php

1 Comment

This is crystal clear and should be the accepted answer to me ^^
14

1. site_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); 

2. base_url

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"); 

Comments

2

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/ 

Comments

1

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.

Comments

1

site_url() is execute index.php that why site_url is better as compare to base_url.

Comments

0

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 .

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.