CI默認的rewrite url中是類似這樣的,例如你的CI根目錄是在/CodeIgniter/下,你的下面的二級url就類似這樣http://localhost/CodeIgniter/index.php/welcome。不太好看,怎么把其中的index.php取掉呢?
1. 打開apache的配置文件,conf/httpd.conf :
LoadModule rewrite_module modules/mod_rewrite.so,把該行前的#去掉。
搜索 AllowOverride None(配置文件中有多處),看注釋信息,將相關.htaccess的該行信息改為AllowOverride All。(PS.測試后,發現該步并非必須,參看http://codeigniter.org.cn/forums/forum.php?mod=viewthread&tid=15553&reltid=4&pre_thread_id=0&pre_pos=2&ext=CB)
2. 在CI的根目錄下,即在index.php,system的同級目錄下,建立.htaccess,直接建立該文件名的不會成功,可以先建立記事本文件,另存為該名的文件即可。內容如下(CI手冊上也有介紹):
1 |
RewriteEngine on |
2 |
RewriteCond $1 !^(index.php|images|robots.txt) |
3 |
RewriteRule ^(.*)$ /index.php/$1 [L] |
如果文件不是在www的根目錄下,例如我的是:http://www.nowamagic.net/CodeIgniter/,第三行需要改寫為RewriteRule ^(.*)$ /CodeIgniter/index.php/$1 [L]。
另外,我的index.php的同級目錄下還有js文件夾和css文件夾,對于這些外部資源需要過濾除去,(意思是排除某些目錄或文件,使得這些目錄不會 rewrite 到 index.php 上),第二行需要改寫為:RewriteCond $1 !^(index.php|images|js|css|robots.txt)
3.將CI中配置文件(system/application/config/config.php)中$config['index_page'] = "index.php";將$config['index_page'] = ""; 。
01 |
/* |
02 |
|-------------------------------------------------------------------------- |
03 |
| Index File |
04 |
|-------------------------------------------------------------------------- |
<code style="margin:0px!important; padding:0px 0.3em 0px 0px!important; border:0px!important; outline:0px!important; text-align:right!important; float:none!important; vertical-align:baseline!important; position:static!important; left:auto!important; top:auto!important; right:auto!important; bottom:auto |