為美觀一些,去掉CI默認url中的index.php處理方案:一個是在linux里面 一個是在windows
里面配置環境
一》在linux里面設置
1.打開apache的配置文件,conf/httpd.conf :
LoadModule rewrite_module modules/mod_rewrite.so,把該行前的#去掉。
搜索 AllowOverride None(配置文件中有多處),看注釋信息,將相關.htaccess的該行信息改為AllowOverride All。【其實apache 默認的都已經打開的】
2.在CI的根目錄下,即在index.php,system的同級目錄下,建立.htaccess,直接建立該文件名不會成功,可以先建立記事本文件,另存為該名的文件即可。內容如下(CI手冊上也有介紹):
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
如果文件不是在www的根目錄下,例如是:http://localhost/nianyi_2011/index.php,第三行
需要改寫為RewriteRule ^(.*)$ /nianyi_2011/index.php/$1 [L]
另外,如果你的網站的根目錄下面還有其他的文件夾,例:js,css,images,config等等文件夾,這需要過濾除去,第二行需要改寫為:
RewriteCond $1 !^index\.php|images|js|css|config|robots\.txt)。
3.將CI中配置文件(application/config/config.php)中$config['index_page'] ="index.php";改寫成$config['index_page'] = "";
4.ok,完成。還要記得重啟apache。
二》在windows里面開發
以上的配置在windows上面是不起任何作用的,所以我們在windows里面需要設置我們的apache的虛擬主機,在apache里面找到Apache-20\conf\extra\httpd-vhosts.conf 這個文件【我用的是服務是PHPnow,很多的人用wamp】,同樣找到此文件,在里面修改,例:
#http://www.PHPnow.org
# filename: httpd-vhosts.conf
<Directory ../vhosts>
AllowOverride All
Order allow,deny
Allow from all
</Directory>
NameVirtualHost *
<VirtualHost *>
DocumentRoot ../htdocs
ServerName default:80
ErrorLog logs/default-error_log
</VirtualHost>
修改成:
#http://www.PHPnow.org
# filename: httpd-vhosts.conf
<Directory ../vhosts>
RewriteEngine on
RewriteCond $1 !^(index\.php|images|js|css|config|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
AllowOverride All
Order allow,deny
Allow from all
</Directory>
NameVirtualHost *
<VirtualHost *>
DocumentRoot ../htdocs
ServerName default:80
ErrorLog logs/default-error_log
</VirtualHost>
也就是把路由寫到這里面來。記得重新啟動服務。
繼續修改你的配置文件:
$config['enable_query_strings'] = true
$config['controller_trigger'] = 'c';
$config['function_trigger'] = 'm';
把$config['enable_query_strings'] 修改成 false 這樣就關閉了查詢字符串形式 URL
現在你的文件的路徑就可以寫得了,例如:http://localhost/nianyi_2011/user/user_point
就相當于先前沒有配置的路由:http://localhost/nianyi_2011/index.php?c=user&m=user_point同樣如果后面有參數的話,只用往后一次累加就好的了。
請注意:有時候我們這樣寫之后樣式文件加載進來會有問題,找不到js css images等等目錄,我們可以配置文件config.php里面設置$config['base_url'] = 'http://localhost/2011_11_cms/';站點的目錄,接著我們在view模板文件<head></head>之間添加<base href="<?=base_url()?>" />,這樣就能加載其他的文件的了