win/Linux下去掉codeigniter框架网站URL中的“index.php”

本文介绍如何在不同服务器环境下(Linux下的Apache和nginx、Windows下的IIS7及Apache)配置URL重写,实现CodeIgniter框架的友好URL。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Linux:

一、Apache

1、启用rewrite模块

手动启用是在Apache配置文件里把“LoadModule rewrite_module modules/mod_rewrite.so”解注释。

一些LAMP套件也提供更方便的模块管理。

2、rewrite规则设置

在 CI 的根目录下,即在 index.php ,system的同级目录下,建立.htaccess,直接建立该文件名不会成功,可以从application或system文件夹里复制一个过来,再修改内容。

内容如下( CI 手册上也有介绍):

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
如果.htaccess文件不是在www的根目录下,例如是: http://localhost/123/ index.php ,第三行需要改写为RewriteRule ^(.*)$ /123/ index.php /$1 [L] 

3、配置codeigniter

找到CI文件夹/application/config/config.php。

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

4、重启Apache

这个各显神通,不累述,如Ubuntu的 $ sudo /etc/init.d/apache2 restart

二、nginx

1、rewrite规则设置

修改nginx配置文件,在nginx.conf的SERVER段中添加如下代码:

location /{
   if (-f $request_filename) {
       expires max;
       break;
   }
   if (!-e $request_filename) {
       rewrite ^/(.*)$ /index.php/$1 last;
   }
}

2、3同上

4、重启nginx

/usr/local/nginx/sbin/nginx -s reload

Windows:

一、IIS7

1、配置web.config

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="OrgPage" stopProcessing="true">
                    <match url="^(.*)$" />
                    <conditions logicalGrouping="MatchAll">
                    <add input="{HTTP_HOST}" pattern="^(.*)$" />
                    <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                    <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                    </conditions>
                    <action type="Rewrite" url="index.php/{R:1}" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>    
</configuration>

2、配置codeigniter

找到CI文件夹/application/config/config.php。

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

二、Apache

1、启用rewrite模块

手动启用是在Apache配置文件里把“LoadModule rewrite_module modules/mod_rewrite.so”解注释。

一些WAMP套件也提供更方便的模块管理。

2、rewrite规则设置

找到Apache的虚拟主机配置文件,如Apache-20\conf\extra\httpd-vhosts.conf。

修改要配置的虚拟主机的Directory标签,如:

<Directory G:/PHP/123>
    #隐藏index.php相关
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/$1 [L]
    Options FollowSymLinks
    #访问权限设置
    #不允许别人修改页面
    AllowOverride None
    #依次考察允许和拒绝ip访问的设置情况
    order allow,deny
    #允许来自所有ip的访问
    Allow from all
    #deny from 202.18 即拒绝来自202.18打头的ip的访问
</Directory>

3、配置codeigniter

找到CI文件夹/application/config/config.php。

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

4、重启Apache

win下Apache有图形界面,很方便。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值