CodeIgniter 删除 index.php apache mod_rewrite

Posted

技术标签:

【中文标题】CodeIgniter 删除 index.php apache mod_rewrite【英文标题】:CodeIgniter remove index.php apache mod_rewrite 【发布时间】:2011-08-10 12:46:41 【问题描述】:

我在使用 Zend 社区服务器和 apache 的 Windows 机器上使用 CodeIgniter。我的 apache 已正确配置为处理 .htaccess 指令,并且启用了 mod_rewrite (httpd.conf):

<Directory />
    Options FollowSymLinks
    AllowOverride FileInfo
    Order deny,allow
    Deny from all
</Directory>

我的根是C:\www\,我的站点位于C:\www\david\,当我输入http://localhost/david/ 时,index.php 与正确的控制器一起加载。我想直接通过http://localhost/david/Articles/ 而不是http://localhost/david/index.php/Articles/ 访问我的东西。为此,我必须使用 apache 重写模块。

为此,我在C:\www\david\ 文件夹中放置了一个.htaccess文件。该文件包含我在here 上找到的代码:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /david/

    #Removes access to the system folder by users.
    #Additionally this will allow you to create a System.php controller,
    #previously this would not have been possible.
    #'system' can be replaced if you have renamed your system folder.
    RewriteCond %REQUEST_URI ^core.*
    RewriteRule ^(.*)$ /index.php?/$1 [L]

    #When your application folder isn't in the system folder
    #This snippet prevents user access to the application folder
    #Submitted by: Fabdrol
    #Rename 'application' to your applications folder name.
    RewriteCond %REQUEST_URI ^application.*
    RewriteRule ^(.*)$ /index.php?/$1 [L]

    #Checks to see if the user is attempting to access a valid file,
    #such as an image or css document, if this isn't true it sends the
    #request to index.php
    RewriteCond %REQUEST_FILENAME !-f
    RewriteCond %REQUEST_FILENAME !-d
    RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>

<IfModule !mod_rewrite.c>
    # If we don't have mod_rewrite installed, all 404's
    # can be sent to index.php, and everything works as normal.
    # Submitted by: ElliotHaughin

    ErrorDocument 404 /index.php
</IfModule> 

我对代码进行了一些更改,因为我的配置与默认配置有些不同。我不在根目录上,所以:'RewriteBase /davidfrancoeur.com/' 我已经重命名了 CodeIgniter 系统文件夹以增加安全性,所以: RewriteCond %REQUEST_URI ^core.* RewriteRule ^(.*)$ /index.php?/$1 [L]

完成此操作后,一切都应该正常工作,我应该能够毫无问题地访问http://localhost/david/Articles/,并且我认为我应该无法直接访问http://localhost/david/core/config/config.php

不幸的是,它不起作用,甚至看起来还没有完成任何重写。你看看我是不是在这里做错了什么?有没有办法知道apache是​​否真的重写了什么?

非常感谢。是的,我查看了我能找到的数百万篇关于此的文章...... :(

编辑

CI 2.0.2、PHP 5.3、Apache 2.2

【问题讨论】:

【参考方案1】:

它会工作.....实际上你的前两个重写条件和规则正在工作,但在第三个重写条件下你允许用户访问请求文件,即使它是核心或系统文件。

    RewriteCond %REQUEST_FILENAME !-f
    RewriteCond %REQUEST_FILENAME !-d
    RewriteCond %REQUEST_FILENAME !^core.*
    RewriteCond %REQUEST_FILENAME !^system.*
    RewriteRule ^(.*)$ index.php?/$1 [L]

编辑:修正“重写”错字

【讨论】:

我仍然可以访问core文件夹,显示app和sys文件夹。【参考方案2】:

仔细查看我的 Zend 社区服务器的 httpd.conf 后,我发现有两个&lt;Directory /&gt;instruction。如问题所示,第一个是空的,第二个看起来像这样:

<Directory "C:\Dropbox\www">
    #
    # Possible values for the Options directive are "None", "All",
    # or any combination of:
    #   Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
    #
    # Note that "MultiViews" must be named *explicitly* --- "Options All"
    # doesn't give it to you.
    #
    # The Options directive is both complicated and important.  Please see
    # http://httpd.apache.org/docs/2.2/mod/core.html#options
    # for more information.
    #
    Options Indexes FollowSymLinks
    #
    # AllowOverride controls what directives may be placed in .htaccess files.
    # It can be "All", "None", or any combination of the keywords:
    #   Options FileInfo AuthConfig Limit
    #
    AllowOverride None
    #
    # Controls who can get stuff from this server.
    #
    Order allow,deny
    Allow from all
</Directory>

我将以下行 AllowOverride None 更改为 AllowOverride All` 并且它起作用了。对于那些想了解更多关于使用 CodeIgniter 重写 URL 的人,请看这里:http://codeigniter.com/wiki/mod_rewrite/

要正确理解AllowOverride的内容:http://httpd.apache.org/docs/2.0/mod/core.html#allowoverride

再次感谢您的帮助!

【讨论】:

【参考方案3】:

ppet 阻止用户访问应用程序文件夹 #提交人:Fabdrol #Rename 'application' 为您的应用程序文件夹名称。 RewriteCond %REQUEST_URI ^应用程序。* RewriteRule ^(.*)$ /index.php?/$1 [L]

#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn't true it sends the
#request to index.php
RewriteCond %REQUEST_FILENAME !-f
RewriteCond %REQUEST_FILENAME !-d
RewriteRule ^(.*)$ index.php?/$1 [L]

【讨论】:

以上是关于CodeIgniter 删除 index.php apache mod_rewrite的主要内容,如果未能解决你的问题,请参考以下文章

Codeigniter 表单打开如何删除 index.php

从 URL 中删除 index.php (CodeIgniter)

无法从 codeigniter 中的 URL 中删除“Index.php”

从 URL 中删除 index.php - Codeigniter 2

无法在 Codeigniter 中删除 index.php

Tomcat删除index.php Codeigniter