我在使用带有apache的Zend Comunity Server的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重写模块。
为此,我在.htaccess
文件夹中放置了C:\www\david\
个文件。此文件包含我在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
答案 0 :(得分:3)
它会工作.....实际上你的前两个重写条件和规则正在运行,但在第三个重写条件下,你允许用户访问请求文件,即使它是核心或系统文件。
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !^core.*
RewriteCond %{REQUEST_FILENAME} !^system.*
RewriteRule ^(.*)$ index.php?/$1 [L]
编辑:修正'重写'拼写错误
答案 1 :(得分:1)
仔细查看Zend社区服务器的httpd.conf后,我意识到有两条<Directory />
指令。第一个是空的,如问题所示,第二个看起来像那样:
<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
再次感谢您的帮助!
答案 2 :(得分:0)
ppet阻止用户访问应用程序文件夹 #Submitted by: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]