Press "Enter" to skip to content

What is Base URL? How to Config and Use

Whenever you want to add images, external css, JavaScript files into your project, you need to specify the location of the file. While developing a CodeIgniter project on your computer, all your project files are located on your computer had disc, that is local host. So you specifies your local computer file location for specifying a file path for the project. But while hosting your project at internet server, all such locations will not work. So you need to specify the internet server location.

As we shown in previous articles, if we do not specify any location for images, actions, etc, then the location lies on the existing code file location. But in some cases, for example while copying all your files on another server, then your current location may be changed and you might get error while specifying file locations.

Here you can learn to get a stable and a fixed location for your project. If we get a stable location, then we will get all other location. So your project can work anywhere.

For example, if somebody ask a route for a place, then you will start telling the route from a fixed location, that may be your standing location. Like this, you can set a stable location for your project files, and all other files based on this default path.

The default base location is known as base URL (base_url), which may be the base directory of your project folder.

How to Use Base URL

You can simply load the base url into your project Controller and Views using the function keyword  base_url(). But before using the function base_url, you need to load the url helper file into your project.


Here I create a new function page named ‘urltest’ on Controller.

public function urltest()
     echo base_url();

Here $this->load->helper(‘url’); loads the helper ‘url’. Note that, this line of code is essential to use base url. Now you can use the keyword ‘base_url()’ anywhere to get the base directory of the project.
Here echo base_url();  prints the base directory of my project named ‘myproject’.



CodeIgniter working with base url

The Uses of Base URL

While submitting a form into to a Controller, we specify the controller page name as form action. If you just specify only the controller name, but in some cases, the page url may be changed due to any error or mistyping of user. So the action code may not find the specified controller name.

So, if you use base_url with your form action, then submission controller location never changes.

<form action="<?php echo base_url()?>index.php/user/userregister" method="post">

or (if the htaccess file is configured)



While using base_url on View section, then you must load ulr helper on corresponding controller page.

You can use base_url for specifying the location of external images, css, JavaScript, etc.

Example shows displaying an image using base_url location:


class User extends CI_Controller
   public function urltest()


<img src=<?php echo base_url().”images/testimage.png”?> >



Sample Output:

Images using base url in CodeIgniter

Customizing Base URL link:

You can change the default path of your base URL. In your CodeIgniter project, there is folder named with‘config’. Inside the folder, open the file ‘config.php’. there you can find the code for customizing the base_url.

$config['base_url'] = '';

Be careful while editing the base_url path.

Be First to Comment

Leave a Reply


%d bloggers like this: