[CodeIgniter] lighttpd rewrite rule && apache mod_rewrite
Jul 9th, 2009 by appleboy 參觀者:13,403Views 機器人:605Views
推到 Twitter!
推到 Plurk!
推到 Facebook!
CodeIgniter 要移除網址列存在的 index.php,apache 必須使用 mod_rewrite 寫入 .htaccess 的方式來達成,lighttpd 也是有支援 rewrite,可以參考官方文件:Module: mod_rewrite,設定方法很容易。
$HTTP["host"] == "mimi.twgg.org" {
server.document-root = "/var/www/html/MLB/"
url.rewrite = (
"^/images/.*$" => "$0",
"^/includes/.*$" => "$0",
"^/(.*)$" => "index.php/$1"
)
accesslog.filename = "/var/log/lighttpd/mimi.twgg.org-access_log"
}
server.document-root = "/var/www/html/MLB/"
url.rewrite = (
"^/images/.*$" => "$0",
"^/includes/.*$" => "$0",
"^/(.*)$" => "index.php/$1"
)
accesslog.filename = "/var/log/lighttpd/mimi.twgg.org-access_log"
}
images 跟 include 是您放圖片或者是 css js 的地方,您也可以開一個資料夾把全部靜態檔案放進去,就可以了,這樣不用設定太多的 rule,apache 的 .htaccess 設定方法如下:
RewriteEngine on
RewriteBase /
RewriteCond $1 !^(index\.php|images|includes)
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
RewriteBase /
RewriteCond $1 !^(index\.php|images|includes)
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
兩種作法很類似,都不難,筆記一下,將來會用到。
可以參考 CodeIgniter 官方文件:CodeIgniter URLs

請問一下版主,我設定好.htaccess
RewriteEngine on
RewriteBase /
RewriteCond $1 !^(index\.php|images|includes)
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
圖片,css,js,的聯結都不見了,圖片的聯結如下,CHJOnline是資料夾的名稱,
http://localhost/CHJOnline/system/application/views/images/right-bg.jpg”
是不是要將圖片都拉出來到CHJOnline的資料夾底下呢?
感謝您
對了,我忘了說一點,
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
改成
RewriteRule ^(.*)$ CHJOnline/index.php/$1 [L,QSA]
然後,controller裡的function about(),
http://localhost/CHJOnline/about
也出現404 Page Not Found
是不是我的設定錯誤了呢?
還麻煩版主回覆一下,感謝您
你應該多瞭解 mod_rewrite 的運作流程,不然這東西會害死一堆初學者,剛開始建議不要用 rewrite 功能,等於是拿石頭丟自己腳。
你好!就是我想請問你
我設定好了mod rewrite 之後
重開apache之後
我只能開啟首頁??
想請問你我可能哪裡有問題!
因為這個問題我困擾了三四天了!!
不好意思很冒昧的打擾您!
請把 .htaccess 內容 po 出來,還有錯誤訊息也順便給我
這樣才可以幫忙判斷,或者是轉到論壇貼問題
http://ci.wuboy.twbbs.org/forum/
Thanks for that short tip
I have no idea what are you writing about but I’m glad for this few lines of example for CI. This solved my problem with more complicated queries in CI.
Thanks.