去掉php框架CI默认url中的index.php

去掉php框架CI默认url中的index.php   

2010-03-17 17:33:07|  分类: php框架ci |字号 订阅

 

CI默认的rewrite url中是类似这样的,例如你的CI根目录是在/CodeIgniter/下,你的下面的二级url就类似这样http://localhost/CodeIgniter/index.php/welcome。不太好看,怎么把其中的index.php取掉呢?

解决方法如下:

去掉 URL 中的 index.php

 

首先,你要清楚自己的 Web 服务器是 Apache,支持 mod_rewrite,并且已经配置好 rewrite 相关的参数。

说明:

Apache Url Rewrite 配置(php伪静态)

检查 conf/httpd.conf 中是否存在如下一段代码:

#LoadModule rewrite_module modules/mod_rewrite.so

把 LoadModule前边的#去掉,然后在网站根目录添加.htaccess(内写伪静态正则代码,这个文件可以在conf/httpd.conf 中的AccessFileName .htaccess指定),也可以直接在conf/httpd.conf中添加伪静态正则代码。然后重启apache(apache -k restart)就可以使用伪静态地址啦。如果没有安装 mod_rewrite,可以重新编译 Apache(或若为win系统建议重新安装),并在原有 configure 的内容中加入 –enable-rewrite=shared,然后再在 Apache 配置文件中加入正则代码。

1、mod_rewrite 简介和配置

Rewirte主要的功能就是实现URL的跳转和隐藏真实地址,基于Perl语言的正则表达式规范。平时帮助我们实现拟静态,拟目录,域名跳转,防止盗链等

2、mod_rewrite 规则的使用

RewriteEngine on

RewriteCond %{HTTP_HOST} !^www.iphpmysql.cn [NC]

RewriteRule ^/(.*) http:// www.iphpmysql.cn / [L]

——–

RewriteEngine on

RewriteRule ^/test([0-9]*).html$ /test.php?id=$1

RewriteRule ^/new([0-9]*)/$ /new.php?id=$1 [R]

3、mod_rewrite 规则修正符

1) R 强制外部重定向

2) F 禁用URL,返回403HTTP状态码。

3) G 强制URL为GONE,返回410HTTP状态码。

4) P 强制使用代理转发。

5) L 表明当前规则是最后一条规则,停止分析以后规则的重写。

6) N 重新从第一条规则开始运行重写过程。

7) C 与下一条规则关联8) T=MIME-type(force MIME type) 强制MIME类型

9) NS  只用于不是内部子请求

10) NC 不区分大小写

11) QSA 追加请求字符串

12) NE 不在输出转义特殊字符   \%3d$1  等价于 =$1


然后,在 CI 根目录下新建立一个配置文件,命名为: .htaccess

在里面这样写:

TEXT

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

TEXT

RewriteCond $1 !^(index\.php|images|robots\.txt)

复制代码

上 面的代码意思是排除某些目录或文件,使得这些目录不会 rewrite 到 index.php 上,这一般用在图片、js、css 等外部资源上。也就是说非 PHP 代码都要排除出去。(这里我排除了 images 目录和 robots.txt 文件,当然 index.php 也应该被排除)

哦,对了,还要修改 config.php 这个文件中的下列内容:

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" 改成 "" 就可以了。

[3]如果经过以上设置还是不成功的话就是Apache配置问题啦。

httpd.conf文件中:

AllowOverride None

改为

AllowOverride All

posted @ 2012-11-21 13:29  简单--生活  阅读(570)  评论(0编辑  收藏  举报
简单--生活(CSDN)