base_url() fails or returns the wrong address in CodeIgniter
Call 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.
Fix it fast
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();2Match what you see, then jump to its fix
- 1
Call to undefined function base_url()CodeIgniter 3: the URL helper is not loadedFix - 2
base_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
You edited App.php, and base_url() did not changeCodeIgniter 4: a value in .env overrides the one in App.phpFix - 4
Links built with site_url() contain index.php, and the server has no rewrite rule for themPages work, but assets 404 because of index.phpFix
- 1
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 browserCSS 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.php2Fix
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/ | headConfirmed by codeigniter.com
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.php2Fix
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="[^"]*"' | headConfirmed by codeigniter.com
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" .env2Fix
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="[^"]*"' | headConfirmed by codeigniter.com
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/null2Fix
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.cssConfirmed by codeigniter.com
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
Android gets HTML instead of JSON from a PHP API
org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
The response body is not JSON.
2 causes, each with a fixBack 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 fixBack end
Apache returns 500 with Premature end of script headers
500 Internal Server Error
Apache ran your script, but the script printed no valid HTTP header before its output.
3 causes, each with a fixBack end
Apache returns 503 or 504 because PHP-FPM did not answer in time
Error dispatching request
Apache sent the request to PHP-FPM and gave up waiting.
3 causes, each with a fixBack end
AWS SDK for PHP cannot get credentials from the instance profile metadata server
Error retrieving credentials from the instance profile metadata server.
The SDK tries the EC2 instance metadata service when it has no valid credentials from anywhere earlier in its chain.
4 causes, each with a fixBack end
AWS SDK for PHP rejects requests: the signature is from the future
Signature not yet current: {date} is still later than {date}
AWS compares the timestamp in your signed request with its own clock.
2 causes, each with a fix