what is the difference between site_url() and base_url()?

Solution 1:

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), '/');
        }

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: https://github.com/EllisLab/CodeIgniter/blob/606fee0e2e0aa6a906db82e77090e91f133d7378/system/core/Config.php

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

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.

http://localhost/CodeIgniter/index.php/controllersName/

will be changed to this

http://localhost/CodeIgniter/controllersName/

Solution 5:

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.