用户
 找回密码
 入住 CI 中国社区
搜索
查看: 159154|回复: 127
收起左侧

去掉 URL 中的 index.php

    [复制链接]
发表于 2007-10-15 13:27:36 | 显示全部楼层 |阅读模式
首先,你要清楚自己的 Web 服务器是 Apache,支持 mod_rewrite,并且已经配置好 rewrite 相关的参数。
什么是 rewrtie 可以 Google 一下。

然后,在 CI 根目录下新建立一个配置文件,命名为: .htaccess
在里面这样写:
  1. RewriteEngine on   
  2. RewriteCond $1 !^(index\.php|images|robots\.txt)   
  3. RewriteRule ^(.*)$ /index.php/$1 [L]
复制代码
就可以去掉 index.php 了。要注意 /index.php/$1 要根据你目录(Web 目录,比如 http://www.domain.com/index.php)的实际情况来定,比如网站根目录是 /ci/index.php 则要写成 /ci/index.php/$1
  1. 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" 改成 "" 就可以了。
 楼主| 发表于 2007-11-7 17:17:02 | 显示全部楼层
感谢楼上提供自己的设置!
 楼主| 发表于 2008-1-3 16:51:25 | 显示全部楼层
我修改了一下,顶上去。。。
 楼主| 发表于 2008-2-15 13:59:32 | 显示全部楼层
是的,但也需要根据你的目录情况进行修改!
具体你应该看看 apache rewrite 的相关手册!这是 apache 的问题,和 CI 没什么关系,呵呵~
 楼主| 发表于 2008-2-15 14:32:24 | 显示全部楼层
IIS 就不是这样了~~
有另外的 url rewrite 方法~~
不过,劝你还是用 apache。

PS: 不一定非要去掉 index.php 吧?带着没有什么不好,顶多就是不好看而已。
 楼主| 发表于 2008-8-26 00:01:10 | 显示全部楼层
你有 mod_rewrite 吗?
 楼主| 发表于 2008-8-28 11:46:45 | 显示全部楼层
之前的有错误,你重新设置一下。
 楼主| 发表于 2008-12-1 01:47:47 | 显示全部楼层
什么是全面的做法?
 楼主| 发表于 2008-12-4 01:09:59 | 显示全部楼层
楼上,请仔细看本楼主的内容!!!
 楼主| 发表于 2009-8-31 15:49:10 | 显示全部楼层
查看 apache 手册,这问题和 CI 无关,CI 没有 rewrite 一样可以使用。

本版积分规则