PHP 之 Ci框架下隐藏index.php

1. 修改 apache 配置文件 

  • 开启重写模块 conf/httpd.conf
去掉前面的#
LoadModule rewrite_module modules/mod_rewrite.so  

对于Ubuntu,需要在apache安装目录下的 mods-enabled 文件夹中创建软连接。

进入 /mods-enabled 下,

cd mods-enabled/
sudo
ln -s ../mods-available/rewrite.load rewrite.load # 要确保 ../mods-available/rewrite.load 文件存在,不存在需要安装rewrite模块

注:apache安装目录下:mods-available---未启用的模块,mods-enabled----已启用的模块

  • AllowOverride None 修改为 AllowOverride All

可以在apache的配置文件 conf/httpd.conf 末尾添加如下代码,或是在 sites-enabled 文件夹下新建个 .conf 文件,文件名随意,也可像 mods-enabled/ 下那样创建链接。

Alias /athena "/home/liuqian/workspace/athena/"    # 依据实际情况改为自己的路径
<Directory "/home/liuqian/workspace/athena/">   # 依据实际情况改为自己的路径
    Options Indexes MultiViews FollowSymLinks
    AllowOverride All    # 主要是这个
    Order allow,deny
    Allow from all
</Directory>

注:apache安装目录下:sites-available---未加载的配置文件,sites-enabled----已加载的配置文件

2. 添加 .htaccess 文件

进入到项目文件夹下,在 index.php 所在的目录下创建 .htaccess 文件

       文件中写如下:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|BJUI|captcha|js|plugins|images|robots\.txt) # index.php的同级目录下还有要过滤除去的文件夹,把名字一并写到这里
RewriteRule ^(.*)$ /athena/index.php/$1 [L]  # 如果网站就在根目录下,就直接写 index.php/$1 [L],我的index.php是在根目录下的athena文件下中

#如果没有安装mod_rewrite模块,所有的404页面都将被 #发送到index.php,此时,程序会像没有设置隐藏时一样运行 ErrorDocument
404 index.php
 

3. 修改Ci配置文件 application/config/config.php

$config['index_page'] = "index.php";

改为:

$config['index_page'] = "";

 

4. 重启apache 就ok了。

sudo apachectl restart  # 这里是ubuntu的重启命令

 

posted on 2018-11-27 14:05  刘[小]倩  阅读(1655)  评论(0编辑  收藏  举报

导航