How to Set Dynamic Base Url in CodeIgniter

How to Set Dynamic Base Url in CodeIgniter

Published at - Aug 13, 2021

To set dynamic base_url in codeigniter 3 we need to follow steps given below.

  1. Open application\config\config.php file.
  2. Replace `$config[‘base_url’]` with following code.
$base_url = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == "on") ? "https" : "http");
$base_url .= "://". @$_SERVER['HTTP_HOST'];
$base_url .=     str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']);
$config['base_url'] = $base_url;

What this code will do ?

This code will automatically get your full base URL with HTTP/HTTPS dynamically. you need to change your base_url every time.

About author

Harendra Kanojiya

Hello, I am Harendra Kumar Kanojiya - Owner of this website and a Fullstack web developer. I have expertise in full-stack web development using Angular, PHP, Node JS, Python, Laravel, Codeigniter and, Other web technologies. I also love to write blogs on the latest web technology to keep me and others updated. Thank you for reading the articles.

Follow Us

Follow us on facebook Click Here

Facebook QR
Scan from mobile
Join our telegram channel Click Here
Telegram QR
Scan from mobile