此文章同步刊登於我的部落格
前言
Codeigniter如果不調整設定,網址中會自帶index.php,為了符合MVC架構,通常會將它去除,步驟如下:
一、開啟伺服器的mod_rewrite
這邊我以Apache作為範例開啟httpd.conf,找到這行:#LoadModule rewrite_module modules/mod_rewrite.so

https://ithelp.ithome.com.tw/upload/images/20210726/20139878y1uX4Whiiu.png
我們要取消註解,所以將這行前面的#去除並重新啟動Apache
二、 確認mod_rewrite是否啟用
運行php函式phpinfo(),並開啟該畫面後,搜尋mod_rewrite

https://ithelp.ithome.com.tw/upload/images/20210726/20139878RW82EbATwl.png
只要在loaded modules欄位內有找到mod_rewrite就代表啟用成功
三、 編輯.htaccess檔案
找到codeigniter根目錄內的.htaccess檔案(若沒有該檔案則建立一個),並在檔案內加入以下內容:
RewriteEngine on
RewriteCond $1 !^(index\.php)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|img|css|js|fonts|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
APACHE
四、 修改config.php檔案
找到application\config\config.php,並找到$config['index_page'] = 'index.php';
並改成:$config['index_page'] = '';
接下來你的網址就可以省略輸入index.php了
環境
- Windows 10
- php 7.3.5
- Apache 2.4.39
- Codeigniter 3.1.6