[Solved] what is the difference between site_url() and base_url()?

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 it’s config.php file (in 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?

Solution #1:

echo base_url(); //
echo site_url(); //

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);
                    $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), '/');
Respondent: Fasil kk

Solution #2:

site_url: Returns base_url + index_page + uri_string

base_url: Returns base_url + uri_string

See source code of both functions at:

Respondent: sikander

Solution #3:

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:

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");
Respondent: Akhilesh krishnan

Solution #4:

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.


will be changed to this

Respondent: Ulett

Solution #5:

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

Respondent: Muhammad Zohaib

Solution #6:

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.

Respondent: Archangel08

Solution #7:

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 .

Respondent: TheVigilant

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Most Popular

To Top
India and Pakistan’s steroid-soaked rhetoric over Kashmir will come back to haunt them both clenbuterol australia bossier man pleads guilty for leadership role in anabolic steriod distribution conspiracy