如何去掉CodeIgniter URL中的index.php

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。直接替換!

2. 在CI的根目錄下,即在index.php,system的同級目錄下,創建.htaccess,直接創建該文件名的不會成功,能夠先創建記事本文件,另存爲該名的文件便可。內容以下(CI手冊上也有介紹):
php

.代碼以下:css


RewriteEngine on 
RewriteCond $1 !^(index\.php|images|robots\.txt) 
RewriteRule ^(.*)$ /index.php/$1 [L]
apache


若是文件不是在www的根目錄下,例如個人是:http://www.nowamagic.net/CodeIgniter/,第三行須要改寫爲RewriteRule ^(.*)$ /CodeIgniter/index.php/$1 [L]。
另外,個人index.php的同級目錄下還有js文件夾和css文件夾,這些須要過濾除去,第二行須要改寫爲:RewriteCond $1 !^(index\.php|images|js|css|robots\.txt)


3.將CI中配置文件(system/application/config/config.php)中$config['index_page'] = "index.php";將$config['index_page'] = ""; 。
app

.代碼以下:less


/*
|--------------------------------------------------------------------------
| Index File
|--------------------------------------------------------------------------

| Typically this will be your index.php file, unless you've renamed it to
| something else. If you are using mod_rewrite to remove the page set this
| variable so that it is blank.

*/
$config['index_page'] = '';
ide


重啓apachethis

相關文章
相關標籤/搜索