win/Linux下去掉codeigniter框架网站URL中的index.php
使用PHP的codeigniter框架后在试验阶段遇到的第一个问题就是,无法隐藏地址栏中的index.php,这让我浑身难受。多方调查之后有了一套解决方案。
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有图形界面,很方便。