How to set proper codeigniter base url?

How to set proper codeigniter base url?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Configuring CodeIgniter Base URL for Subdirectory Deployments

When migrating a CodeIgniter application from a root-level domain like testurl.com to a subdirectory structure such as someurl.com/mysite/, the routing behavior often breaks unexpectedly. If you attempt to access /home/test on your production server and are redirected to someurl.com/home/test instead of the correct someurl.com/mysite/home/test, the issue lies in how CodeIgniter interprets its base URL configuration versus the actual document root.

The most common mistake occurs when developers configure the $config['base_url'] setting in application/config/config.php. In your specific case, you likely set:

$config['base_url'] = 'http://someurl.com/mysite/'; // Note: The source used a raw string without protocol

If you omit the full URL or misconfigure it, CodeIgniter may not generate links correctly. However, simply updating this variable is often insufficient if your server environment variables are not aligned with the new path structure. CodeIgniter relies heavily on $_SERVER['HTTP_HOST'] and $_SERVER['SCRIPT_NAME'] to determine where it sits within the web server hierarchy.

To fix this issue without relying solely on hardcoded URLs in your config file, you must ensure that the server environment variables are set correctly. When Apache or Nginx serves the application from a subdirectory, they should ideally pass the full host and script name to PHP via server variables. If these variables are stripped or normalized by the web server (common in some hosting environments), CodeIgniter will default to a root-level URL generation.

The most robust solution involves adjusting the .htaccess file or Nginx configuration to normalize the request path. By rewriting rules, you can force PHP to see the full URI including the subdirectory. Alternatively, you can use a more explicit approach by setting the base URL dynamically based on environment detection.

In your application/config/config.php, consider using a conditional block that detects whether the application is running in production:

if (ENVIRONMENT === 'production') {
    $config['base_url'] = 'https://someurl.com/mysite/';
} else {
    $config['base_url'] = 'http://testurl.com/';
}

Make sure to include the protocol (http:// or https://) and end with a trailing slash. CodeIgniter 2.x and earlier versions are sensitive to this; omitting the slash can cause link generation errors in views and controllers.

Another potential pitfall is the presence of index.php. If your application is deployed without an index.php file at the root of the subdirectory, you may need to adjust the index_page config setting as well:

$config['index_page'] = '';

This tells CodeIgniter not to prepend index.php to generated URLs, which is necessary for clean routing in production environments.

If you're still encountering issues after adjusting these settings, inspect your .htaccess file to ensure it contains the standard CodeIgniter rewrite rules:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]

These rules ensure that all requests are routed through index.php, allowing CodeIgniter to correctly parse the incoming URI and generate proper base URLs.

For advanced scenarios where you want to avoid hardcoding the full URL in config files, you can leverage the base_url() helper function with a custom configuration. This allows for more flexibility across different environments without duplicating logic.

By aligning your server configuration, environment variables, and CodeIgniter settings, you ensure that your application generates correct URLs regardless of where it is deployed. This approach minimizes maintenance overhead and prevents common redirect loops that plague subdirectory deployments.

Our network