解析如何去掉CodeIgniter URL中的index.php

yipeiwu_com6年前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

相关文章

PHP7.1安装yaf扩展的方法

把PHP命令加到系统 我的PHP安装目录是/usr/local/webserver/php,所以phpize是/usr/local/webserver/php/bin/phpize,但是...

深入PHP magic quotes的详解

深入PHP magic quotes的详解

特地查看了下手册,关于php  magic quotes,常见的几个设置如下,magic_quotes_gpc,magic_quotes_sybase,magic_quote_...

深入解析PHP内存管理之谁动了我的内存

首先让我们看一个问题: 如下代码的输出,复制代码 代码如下:var_dump(memory_get_usage());$a = "laruence";var_dump(memory_ge...

详解PHP多个进程配合redis的有序集合实现大文件去重

详解PHP多个进程配合redis的有序集合实现大文件去重

1.对一个大文件比如我的文件为 -rw-r--r-- 1 ubuntu ubuntu 9.1G Mar 1 17:53 2018-12-awk-uniq.txt 2.使用split命令切...

PHP设计模式之适配器模式定义与用法详解

本文实例讲述了PHP设计模式之适配器模式定义与用法。分享给大家供大家参考,具体如下: 适配器很容易理解, 大多数人家庭都有手机转接器, 用来为移动电话充电,这就是一种适配器. 如果只有U...