在 CodeIgniter 開發中,site_url() 與 base_url() 是兩個常用但易混淆的 URL 產生函式。其關鍵差異在於是否包含 index_page(預設為 index.php)。
$config['base_url'] = "https://domain.com/"; $config['index_page'] = "index.php";
| 呼叫方式 | 實際產生 URL | 說明 |
|---|---|---|
site_url("news/php/2") | https://domain.com/index.php/news/php/2 | 包含 index.php |
base_url("news/php/2") | https://domain.com/news/php/2 | 不含 index.php |
index.php 的環境。若在 config.php 將 $config['index_page'] = ''(配合 Apache mod_rewrite 或 Nginx rewrite 隱藏 index.php),兩者輸出將趨近一致,但語意上仍建議依用途區分使用,維持程式碼可讀性。