codeigniter:去掉 URL 中的 index.php
去掉 URL 中的 index.php
首先,你要清楚自己的 Web 服务器是 Apache,支持 mod_rewrite,并且已经配置好 rewrite 相关的参数。
什么是 rewrtie 可以 Google 一下。
然后,在 CI 根目录(与index.php同级)
下新建立一个配置文件,命名为: .htaccess
在里面这样写:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1
就可以去掉 index.php 了。要注意 /index.php/$1 要根据你目录(Web 目录,比如 http://www.domain.com/index.php)的实际情况来定,比如网站根目录是 /ci/index.php 则要写成 /ci/index.php/$1
RewriteCond $1 !^(index\.php|images|robots\.txt)
上面的代码意思是排除某些目录或文件,使得这些目录不会 rewrite 到 index.php
上,这一般用在图片、js、css 等外部资源上。也就是说非 PHP 代码都要排除出去。(这里我排除了 images 目录和
robots.txt 文件,当然 index.php 也应该被排除)
哦,对了,还要修改 config.php 这个文件中的下列内容:
/*
|--------------------------------------------------------------------------
| 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" 改成 "" 就可以了。
另一篇:
首先,设置apache的配置文件
httpd.conf
,该文件默认在
/apache/conf/
目录下。
开启
mod_rewrite.so
模块,然后将需要进行rewrite的目录属性设置为
AllowOverride All
;
http://codeigniter.org.cn/forums/attachments/month_0901/20090120_6ee1927942c4768f72c2BEDQTCeFnFPj.png
http://codeigniter.org.cn/forums/attachments/month_0901/20090120_42c398f3e46fef9c51a7VKae6ZknyefR.png
其次:设置Codeigniter的
http://codeigniter.org.cn/forums/images/default/attachimg.gif
下载
(3.43 KB)
2009-1-20 11:49
http://codeigniter.org.cn/forums/images/default/attachimg.gif
下载
(6.23 KB)
2009-1-20 11:41
config.php
文件,该文件默认在
/system/application/config
目录下。
将其中的
$config['index_page'] = "index.php"
一项改为
$config['index_page'] = "";
http://codeigniter.org.cn/forums/attachments/month_0901/20090120_267866527ab09de03cdaWGewuuzRJ5zA.png
最后,编写.htaccess文件,将文件放在CI目录即可。
我这里给出我的.htaccess文件内容供大家参考。
<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
#设置根目录
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
</IfModule>
http://codeigniter.org.cn/forums/images/default/attachimg.gif
下载
(2.36 KB)
2009-1-20 11:49
注:如果没有设置根目录的话,就需要在规则的index.php前面将目录加上
给出最终效果图:
http://codeigniter.org.cn/forums/images/default/attachimg.gif
http://codeigniter.org.cn/forums/attachments/month_0901/20090120_9a201fdb7aa2393d5405WR9LxzbCaM9k.png
页:
[1]