Set Dynamic Base Url in CodeIgniter

Set Dynamic Base Url in CodeIgniter

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

Stefan Bogdanescu

Founder & Senior Architect

Set Dynamic Base Url in CodeIgniter

Handling the base URL in a legacy PHP framework like CodeIgniter requires a different mindset than modern MVC frameworks. Unlike Laravel or Symfony, where dependency injection and strict interfaces often enforce configuration patterns, CodeIgniter relies heavily on environment variables and global configuration files. When you face scenarios like switching between localhost for development, supporting multiple public domains, or dealing with volatile IP addresses via DHCP, you need a robust strategy that isolates the base URL from your core application logic.

The most common pitfall developers encounter is hardcoding the base path in controllers or views. If you define $config['base_url'] directly in application/config/config.php, you are coupling your production environment to a specific string. This breaks immediately when deploying from a local machine with an .localhost entry to a live server, or if your hosting provider assigns a new IP address to your virtual host.

To solve this, you must leverage CodeIgniter's native support for environment-specific configuration files. The framework loads config.php first, but it then looks for a corresponding file named after your current environment (e.g., config.local.php, config.test.php, or config.production.php). This allows you to define the base URL dynamically based on where the application is running without touching the core codebase.

Environment-Specific Configuration Files

The cleanest approach is to separate your configuration by environment. Create a file named application/config/config.local.php for development and application/config/config.production.php for your live server.

Inside config.local.php, you might set the base URL to match your local environment, potentially including port numbers:

$config['base_url'] = 'http://localhost:8080/';
$config['index_page'] = '';

Conversely, in config.production.php, you define the live domain. Crucially, this file should not reference the local IP address if the production server uses a static domain name or a dynamic DNS service. Even if your local machine gets a new DHCP IP, changing config.local.php ensures your development environment adapts without breaking the production build.

This separation directly addresses the first requirement: resetting the URL when moving from localhost to live. You simply swap which configuration file is loaded by your deployment script or set an environment variable that CodeIgniter respects. Note that CodeIgniter 3 does not automatically detect these files based on PHP constants without a helper; you often need to explicitly load them or rely on the standard ENVIRONMENT constant being set correctly in your bootstrap file before the config array is merged.

Handling Multiple Domains and Subdomains

The second challenge—accessing a single application from multiple domains (e.g., app.example.com and sub.app.example.com)—requires dynamic URL resolution. Hardcoding one of these domains will lead to relative link issues or incorrect canonical tags on the others.

In CodeIgniter, you can detect the current host dynamically within your bootstrap file or a library loaded early in the request lifecycle. You can use PHP's built-in $_SERVER['HTTP_HOST'] to construct the base URL programmatically. This ensures that regardless of which domain a user types into their browser, the base URL reflects exactly what was requested.

However, be cautious with subdomains. If your application runs on multiple subdomains, ensure your routing logic and security headers (like HSTS) are configured to accept traffic from all valid entries. You can define an array in your production config that lists allowed hosts if you need strict validation, but for a generic multi-domain setup, relying on the incoming request header is usually sufficient.

The DHCP and IP Address Consideration

The third scenario involves local environments where the IP address changes due to DHCP. In these cases, using $_SERVER['REMOTE_ADDR'] or the output of phpinfo() is dangerous because it reflects the client's IP or the server's current public interface, not necessarily the intended base URL for routing purposes.

If you are running a local web server (like XAMPP or MAMP) that binds to a specific interface, the best practice is to configure your local host file (/etc/hosts on Linux/Mac or C:\Windows\System32\drivers\etc\hosts on Windows) to resolve a consistent hostname (e.g., dev.myapp.local) to your current local IP. Then, set your config.local.php base URL to that hostname rather than the raw IP address like http://192.168.1.x/.

When the DHCP lease expires and the IP changes, you update your hosts file entry once, or better yet, use a script to automatically regenerate the hosts entry with the new IP. Your application then continues to use the stable hostname, rendering the base URL dynamic relative to the network state without requiring code changes.

Implementation Trade-offs

While using environment files is robust, it introduces a dependency on the ENVIRONMENT constant being set correctly before the configuration array is merged. If you deploy via CLI or a custom entry point, ensure you explicitly set $config['base_url'] in your main bootstrap file if automatic detection fails.

Another trade-off is performance. Constructing URLs dynamically every request adds negligible overhead, but if you are building complex routing tables based on the base URL, consider caching the resolved host. For most standard CodeIgniter applications, however, reading from $_SERVER['HTTP_HOST'] at the start of a controller or model remains efficient and reliable.

If your project eventually migrates to a newer framework like Laravel, you will find these concepts integrated more tightly into the framework's core, but mastering them in CodeIgniter provides a solid foundation for managing URL structures across any PHP ecosystem.

Our network