去掉 CodeIgniter(CI)默认 URL 中 index.php 的步骤:
- 打开 Apache 的配置文件
conf/httpd.conf:
LoadModule rewrite_module modules/mod_rewrite.so
把该行前的 # 去掉。
搜索 AllowOverride None(配置文件中有多处),看注释信息,将相关 .htaccess 的该行信息改为:
AllowOverride All
- 在 CI 的根目录下,即在
index.php、system的同级目录下,建立.htaccess。直接建立该文件名的不会成功,可以先建立记事本文件,另存为该名的文件即可。内容如下(CI 手册上也有介绍):
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
如果文件不是在 www 的根目录下,例如我的是:
http://localhost/ci_demo_1/index.php/
第三行需要改写为:
RewriteRule ^(.*)$ /CI/index.php/$1 [L]
另外,我的 index.php 的同级目录下还有 assets 文件夹,这些需要过滤除去,第二行需要改写为:
RewriteCond $1 !^(index\.php|images|assets|robots\.txt)
- 将 CI 中配置文件
application/config/config.php中:
$config['index_page'] = "index.php";
改成:
$config['index_page'] = "";
重启 Apache,完成。
PHP 框架 CI 去 index.php 的方法。
网上有很多方法都要引入 .htaccess 文件,如果是在测试环境下,动态和静态的文件放到一块,可能测试会有一定的问题(由于全部定向到 index.php),静态网页访问不了。
这里提供一种方法,只需要修改 http.conf 文件。
步骤:
- 在配置虚拟目录下加入
<Directory />
Options Indexes FollowSymLinks
AllowOverride all
Order allow,deny
Allow from all
</Directory>
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^/script/(.*) /script/$1 [L]
RewriteRule ^(.*)$ /index.php?/$1 [L]
</IfModule>
- 将下面这行前面的
;去掉
LoadModule rewrite_module modules/mod_rewrite.so
- 重启 Apache 就可以了,无需加入
.htaccess文件。