去掉 URL 中的 index.php
首先,你要清楚自己的 Web 服務(wù)器是 Apache,支持 mod_rewrite,并且已經(jīng)配置好 rewrite 相關(guān)的參數(shù)。
什么是 rewrtie 可以 Google 一下。
然后,在
CI 根目錄(與index.php同級)
下新建立一個配置文件,命名為:
.htaccess
在里面這樣寫:
RewriteEngine on RewriteCond $1 !^(index\.php|images|robots\.txt) RewriteRule ^(.*)$ /index.php/$1 [L]?
就可以去掉 index.php 了。要注意 /index.php/$1 要根據(jù)你目錄(Web 目錄,比如 http://www.domain.com/index.php)的實際情況來定, 比如網(wǎng)站根目錄是 /ci/index.php 則要寫成 /ci/index.php/$1
RewriteCond $1 !^(index\.php|images|robots\.txt)?
上面的代碼意思是排除某些目錄或文件,使得這些目錄不會 rewrite 到 index.php 上,這一般用在圖片、js、css 等外部資源上。也就是說非 PHP 代碼都要排除出去。(這里我排除了 images 目錄和 robots.txt 文件,當(dāng)然 index.php 也應(yīng)該被排除)
哦,對了,還要修改 config.php 這個文件中的下列內(nèi)容:
/* |-------------------------------------------------------------------------- | 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'] = "index.php";?
把其中的 "index.php" 改成 "" 就可以了。
另一篇:
首先,設(shè)置apache的配置
文件
httpd.conf
,該文件默認(rèn)在
/apache/conf/
目錄下。
開啟
mod_rewrite.so
模塊,然后將需要進行rewrite的目錄屬性設(shè)置為
AllowOverride All
;
其次:設(shè)置Codeigniter的
config.php
文件,該文件默認(rèn)在
/system/application/config
目錄下。
將其中的
$config['index_page'] = "index.php"
一項改為
$config['index_page'] = "";
最后,編寫.htaccess文件,將文件放在CI目錄即可。
我這里給出我的.htaccess文件內(nèi)容供大家參考。
<IfModule mod_rewrite.c> <Files ~ "^.(htaccess|htpasswd)$"> deny from all </Files> Options -Indexes Options +FollowSymLinks #允許解析文件中的SSI指令 Options +Includes #定義404,500錯誤頁面 ErrorDocument 404 /404.htm ErrorDocument 500 /404.htm #定義目錄索引頁面 DirectoryIndex index.php order deny,allow RewriteEngine on #設(shè)置根目錄 RewriteBase /www/ci_170/ #去掉鏈接地址中index.php字符串 RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteCond $1 !^(index.php|images|robots\.txt) RewriteRule ^(.*)$ index.php/$1 [L] </IfModule>?
注:如果沒有設(shè)置根目錄的話,就需要在規(guī)則的index.php前面將目錄加上
給出最終效果圖:
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
