如何去掉CodeIgniter URL中的index.php
发布于 2014-08-09 23:46:54 | 105 次阅读 | 评论: 0 | 来源: 网友投递
CodeIgniter开源PHP开发框架CodeIgniter 是一个简单快速的PHP MVC框架。EllisLab 的工作人员发布了 CodeIgniter。许多企业尝试体验过所有 PHP MVC 框架之后,CodeIgniter 都成为赢家,主要是由于它为组织提供了足够的自由支持,允许开发人员更迅速地工作。
本文讲解了如何通过配置apache的url重写规则+修改ci框架的路由规则去掉 ci框架url中的index.php,实现伪静态.感兴趣的同学参考下吧.
问题:
CI默认的rewrite url中是类似这样的,例如你的CI根目录是在/CodeIgniter/下,你的下面的二级url就类似这样http://localhost/CodeIgniter/index.php/welcome。不太好看,怎么把其中的index.php取掉呢?
1. 打开apache的配置文件,conf/httpd.conf :
LoadModule rewrite_module modules/mod_rewrite.so,把该行前的#去掉。
搜索 AllowOverride None(配置文件中有多处),看注释信息,将相关.htaccess的该行信息改为AllowOverride All。
2. 在CI的根目录下,即在index.php,system的同级目录下,建立.htaccess,直接建立该文件名的不会成功,可以先建立记事本文件,另存为该名的文件即可。内容如下(CI手册上也有介绍):
代码如下:
RewriteEngine on
RewriteCond $1 !^(index.php|images|robots.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
如果文件不是在www的根目录下,例如我的是:http://www.nowamagic.net/CodeIgniter/,第三行需要改写为RewriteRule ^(.*)$ /CodeIgniter/index.php/$1 [L]。
另外,我的index.php的同级目录下还有js文件夹和css文件夹,这些需要过滤除去,第二行需要改写为:RewriteCond $1 !^(index.php|images|js|css|robots.txt)
3.将CI中配置文件(system/application/config/config.php)中$config['index_page'] = "index.php";将$config['index_page'] = ""; 。
代码如下:
/*
|--------------------------------------------------------------------------
| 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'] = '';
重启apache
相关阅读:
如何去掉CodeIgniter URL中的index.php
CI框架中使用 .htaccess 隐藏url中index.php
codeigniter框框中如何使用框架的session
如何让CodeIgniter在IDE中有代码提示功能
CodeIgniter框架中使用cookie的三种方式
CodeIgniter针对lighttpd服务器URL重写的方法
使用配置类定义Codeigniter全局变量的方法
CodeIgniter中文乱码的两种解决办法
CodeIgniter框架中_remap()2种使用方法
CI框架学习(二)入口文件index.php
Codeigniter发送邮件的方法
CodeIgniter URL