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 [L]
 

就可以去掉 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
开启mod_rewrite.so模块.png

rewrite的目录属性设置为AllowOverride All.png
其次:设置Codeigniter的

下载 (3.43 KB)
2009-1-20 11:49

下载 (6.23 KB)
2009-1-20 11:41

config.php 文件,该文件默认在 /system/application/config 目录下。
将其中的
$config['index_page'] = "index.php" 一项改为 $config['index_page'] = "";
设置Codeigniter的config.php文件.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 [L]

</IfModule>
 


下载 (2.36 KB)
2009-1-20 11:49

注:如果没有设置根目录的话,就需要在规则的index.php前面将目录加上

给出最终效果图:

去index.php效果图.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值