為美觀一些,去掉CI默認(rèn)url中的index.php處理方案:一個(gè)是在linux里面 一個(gè)是在windows
里面配置環(huán)境
一》在linux里面設(shè)置
1.打開(kāi)apache的配置文件,conf/httpd.conf :
LoadModule rewrite_module modules/mod_rewrite.so,把該行前的#去掉。
搜索 AllowOverride None(配置文件中有多處),看注釋信息,將相關(guān).htaccess的該行信息改為AllowOverride All。【其實(shí)apache 默認(rèn)的都已經(jīng)打開(kāi)的】
2.在CI的根目錄下,即在index.php,system的同級(jí)目錄下,建立.htaccess,直接建立該文件名不會(huì)成功,可以先建立記事本文件,另存為該名的文件即可。內(nèi)容如下(CI手冊(cè)上也有介紹):
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
如果文件不是在www的根目錄下,例如是:http://localhost/nianyi_2011/index.php,第三行
需要改寫(xiě)為RewriteRule ^(.*)$ /nianyi_2011/index.php/$1 [L]
另外,如果你的網(wǎng)站的根目錄下面還有其他的文件夾,例:js,css,images,config等等文件夾,這需要過(guò)濾除去,第二行需要改寫(xiě)為:
RewriteCond $1 !^index\.php|images|js|css|config|robots\.txt)。
3.將CI中配置文件(application/config/config.php)中$config['index_page'] ="index.php";改寫(xiě)成$config['index_page'] = "";
4.ok,完成。還要記得重啟apache。
二》在windows里面開(kāi)發(fā)
以上的配置在windows上面是不起任何作用的,所以我們?cè)?strong style="color: white; background-color: #880000;">windows里面需要設(shè)置我們的apache的虛擬主機(jī),在apache里面找到Apache-20\conf\extra\httpd-vhosts.conf 這個(gè)文件【我用的是服務(wù)是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>
也就是把路由寫(xiě)到這里面來(lái)。記得重新啟動(dòng)服務(wù)。
繼續(xù)修改你的配置文件:
$config['enable_query_strings'] = true
$config['controller_trigger'] = 'c';
$config['function_trigger'] = 'm';
把$config['enable_query_strings'] 修改成 false 這樣就關(guān)閉了查詢字符串形式 URL
現(xiàn)在你的文件的路徑就可以寫(xiě)得了,例如:http://localhost/nianyi_2011/user/user_point
就相當(dāng)于先前沒(méi)有配置的路由:http://localhost/nianyi_2011/index.php?c=user&m=user_point同樣如果后面有參數(shù)的話,只用往后一次累加就好的了。
請(qǐng)注意:有時(shí)候我們這樣寫(xiě)之后樣式文件加載進(jìn)來(lái)會(huì)有問(wèn)題,找不到j(luò)s css images等等目錄,我們可以配置文件config.php里面設(shè)置$config['base_url'] = 'http://localhost/2011_11_cms/';站點(diǎn)的目錄,接著我們?cè)趘iew模板文件<head></head>之間添加<base href="<?=base_url()?>" />,這樣就能加載其他的文件的了