Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

base_url() fails or returns the wrong address in CodeIgniter

ErrorCall to undefined function base_url()

Short answer

base_url() lives in the URL helper, so in CodeIgniter 3 an undefined-function error means the helper is not loaded. A wrong address means the configured base URL is wrong or overridden. In CodeIgniter 4 set baseURL with a trailing slash. If assets 404 while pages work, compare base_url() with site_url() and the index.php rewrite.

  • Applies to CodeIgniter 3.1, CodeIgniter 4.5 to 4.6, PHP 8.3, Apache with mod_rewrite
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Print what the framework thinks the base URL is, from a controller or a view. The output separates a missing helper from a wrong setting.

    <?php
    // temporary debug line, remove after use
    echo base_url(), ' | ', site_url();
  2. 2Match what you see, then jump to its fix

    1. 1Call to undefined function base_url()CodeIgniter 3: the URL helper is not loadedFix
    2. 2base_url() prints a different host, a different port or a missing sub-folderThe configured base URL does not match how you open the siteFix
    3. 3You edited App.php, and base_url() did not changeCodeIgniter 4: a value in .env overrides the one in App.phpFix
    4. 4Links built with site_url() contain index.php, and the server has no rewrite rule for themPages work, but assets 404 because of index.phpFix

What does the error look like?

  • Call to undefined function base_url()
  • base_url() returns a different host, port or folder than the one in the browser
  • CSS and images 404 while pages open

Cause 1 of 4

CodeIgniter 3: the URL helper is not loaded

1Check

In CodeIgniter 3, base_url() belongs to the URL helper, which you load with $this->load->helper('url'). Check whether the controller or the autoload file does that.

grep -rn "helper('url')\|helper(\"url\")" application/controllers application/config/autoload.php
grep -n "autoload\['helper'\]" application/config/autoload.php

2Fix

Load the helper in the controller. To load it everywhere, add url to the helper list in application/config/autoload.php; the comments in that file show the format, and the name goes in without .php.

// controller
$this->load->helper('url');

3Verify

The first-step line now prints two addresses.

curl -s http://localhost/ | head

Confirmed by codeigniter.com

Did this fix it?

Cause 2 of 4

The configured base URL does not match how you open the site

1Check

Both base_url() and site_url() read the site URL from your config. CodeIgniter 4 sets it in baseURL in app/Config/App.php; CodeIgniter 3 keeps its base URL in application/config/config.php. Compare with the address in your browser.

grep -n "base_url" application/config/config.php
grep -n "baseURL" app/Config/App.php

2Fix

Set the real address. In CodeIgniter 4 the docs say to always use a trailing slash. For a site in a sub-folder, include the folder.

// CodeIgniter 4: app/Config/App.php
public string $baseURL = 'https://www.example.com/ci-blog/';

3Verify

The debug line prints the address you open in the browser.

curl -s http://localhost:8080/ | grep -o 'href="[^"]*"' | head

Confirmed by codeigniter.com

Did this fix it?

Cause 3 of 4

CodeIgniter 4: a value in .env overrides the one in App.php

1Check

The configuration docs say values from .env take priority, and a short prefix such as app. maps to the App config class. So app.baseURL in .env wins over the property.

grep -n "app.baseURL" .env

2Fix

Change the value in .env (or delete the line), and keep the trailing slash.

# .env
app.baseURL = 'http://localhost:8080/'

3Verify

Reload the page and compare the debug line.

curl -s http://localhost:8080/ | grep -o 'href="[^"]*"' | head

Confirmed by codeigniter.com

Did this fix it?

Cause 4 of 4

Pages work, but assets 404 because of index.php

1Check

site_url() adds the index page (index.php by default) and base_url() leaves it out. Without a server rewrite, a path without index.php is not routed to the framework.

grep -n "indexPage\|index_page" app/Config/App.php application/config/config.php 2>/dev/null

2Fix

Use base_url() for files such as CSS and images, and site_url() for routes. To drop index.php from routes, enable mod_rewrite and a .htaccess with rewrite rules that skip existing files and directories, as the URL guide shows.

<link rel="stylesheet" href="<?= base_url('assets/app.css') ?>">
<a href="<?= site_url('news/local/123') ?>">News</a>

3Verify

The asset URL returns 200.

curl -I http://localhost/assets/app.css

Confirmed by codeigniter.com

Did this fix it?

How do I stop it coming back?

  • Set the base URL per environment (.env in CodeIgniter 4) instead of hard-coding it.
  • Always end baseURL with a slash.
  • Use base_url() for files and site_url() for routes, and never write the host by hand in a view.
  • Load the url helper in the autoload list once, so every controller has it.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

Apache returns 403 when a URL contains %3F

a request like /page%3Fname=test is refused while /page?name=test works

Recent Apache httpd blocks a rewrite when the request contains an encoded question mark (%3F) and the rewritten target has a literal ?.

2 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.