[Codeigniter] remove index.php & 圖片無法讀取問題

CodeIgniter 移除 URL 中 index.php 並修復上傳圖片無法讀取的問題,調整 .htaccess 的 RewriteRule 加入 REQUEST_FILENAME 條件即可解決。

首先,是先簡單說明使用 Codeigniter framework 時移除 URL 中的 index.php
在 Codeigniter 的根目錄新增一 .htaccess 檔案
並放入以下內容

1
2
3
RewriteEngine on  
RewriteCond $1 !^(index\.php|js|robots\.txt|css)  
RewriteRule ^(.*)$ index.php/$1 [L]  

另外還要修改 Codeigniter 的 config.php 設定

$config['index_page'] = ''";

不過上面的 rewrite rule 有些問題,
情境是這樣的:
我在 Codeigniter 根目錄新增一資料夾為 uploads ,
放置上傳的圖片與影片,
但是因為 rewrite rule routing 的關係沒辦法讀取到圖片,
因此我將 .htaccess 檔修改如下:

1
2
3
4
5
RewriteEngine on  
RewriteCond $1 !^(index\.php|images|css|js|robots\.txt|favicon\.ico)  
RewriteCond %{REQUEST_FILENAME} !-f  
RewriteCond %{REQUEST_FILENAME} !-d  
RewriteRule ^(.*)$ ./index.php?/$1 [L,QSA]  

主要是在三四行的 %{REQUEST_FILENAME}
這樣修改完就解決問題了!

ref: CodeIgniter 如何去掉 URL 中的 index.php

comments powered by Disqus
Powered by Hugo. Theme Stack. All Rights Reserved.