CI去掉 URL 中的 index.php

首先,你要清楚自己的 Web 服务器是 Apache,支持 mod_rewrite

查找httpd.conf中是否开启了mod_rewrite.so

然后,在 CI 根目录下新建立一个配置文件,命名为: .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" 改成 "" 就可以了。

时不我待,不负韶华!立刻行动!不吃学习的苦就会吃生活的苦!
原文地址:https://www.cnblogs.com/zrp2013/p/3556772.html