Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save robhi2031/2bcc8f85da889e2c5e958793a10eac21 to your computer and use it in GitHub Desktop.
Save robhi2031/2bcc8f85da889e2c5e958793a10eac21 to your computer and use it in GitHub Desktop.
Setting Dynamic base URL in CodeIgniter 4

Setting Dynamic base URL in CodeIgniter 4

  1. Go to the App.php file;
  2. In section public $baseURL = 'http://localhost:8080/';, and replace with this script:
    public $baseURL;
    public function __construct()
    {
        if (!empty($_SERVER['HTTPS'])){
            $root = "https://".$_SERVER['HTTP_HOST'];
            $root .= dirname($_SERVER['SCRIPT_NAME']);
            $this->baseURL = $root."/";
        }else{
            $root = "http://".$_SERVER['HTTP_HOST'];
            $root .= dirname($_SERVER['SCRIPT_NAME']);
            $this->baseURL = $root."/";
        }
    }
  1. done, and good luck.

This really helped me out of the many ways I found to make the base url in codeigniter 4 dynamic.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment