0

I am creating a project in codeIgniter, using wamp to develop on locally (Apache 2.4). Throughout the project I've had mod_rewrite switched on to remove the index.php that appears in urls in codeigniter by default by using the .htaccess file in the codeigniter project directory i.e. C:\wamp\www\codeIgniter\.htaccess.

We needed windows authentication in the application so after finally managing to get the ldap and sspi modules working, I applied the following to the httpd.conf file in c:wamp\bin\apache\Apache2.4.4\conf\httpd.conf

<Directory "c:/wamp/www/codeIgniter">
 AllowOverride None
 Options None
 Order allow,deny
 Allow from all

 AuthName "protected"
 AuthType SSPI
 SSPIAuth On
 SSPIAuthoritative On
 SSPIOmitDomain On 

 require valid-user
</Directory>

I got this config here.

After making these change to the httpd.conf file, the mod_rewrite rules seem to no longer apply and I must write index.php after the root of the project to get a url to load.

After reading some of the Apache documentation on setting up httpd.conf config and .htaccess files it recommends that .htaccess files should not be used if access to the server config is possible:

In general, you should only use .htaccess files when you don't have access to the main server configuration file. There is, for example, a common misconception that user authentication should always be done in .htaccess files, and, in more recent years, another misconception that mod_rewrite directives must go in .htaccess files. This is simply not the case. You can put user authentication configurations in the main server configuration, and this is, in fact, the preferred way to do things. Likewise, mod_rewrite directives work better, in many respects, in the main server configuration. Link

So firstly I'm trying to understand how I can get my mod_rewrite rules working again happily alongside my sspi stuff and secondly how I can move my mod_rewrite rules from .htaccess to my server config file httpd.conf.

I have tried switching AllowOverride All in the server config as in the above-mentioned code but this seems to make everything inaccessible. I have also tried changing Order allow,deny and Allow from all to Require all granted but this seems to cause similar issues too. Adding my mod_rewrite rules to the in the server config also caused everything to be inaccessible. Any guidance would be most appreciated! Thank you.

My .htaccess is as follows:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /codeIgniter
#RewriteBase /codeIgniter

#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} ^system.*
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 acces 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 !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

4

2 回答 2

1

阅读AllowOverride的工作原理。 None禁用.htaccess文件。你FileInfo至少需要。

Htaccess 文件处理确实会导致性能下降,因为每个请求都会读取和解析访问文件,而在 Apache 启动时会读取和缓存服务器和虚拟主机规则。但是,这必须与能够根据每个请求规则更改规则处理的灵活性进行交易。对于本地开发,您可以忽略此建议。

还阅读了重写规则的每个目录处理之间的差异。特别是,删除/任何 RewriteRule 匹配模式上的前导字符。相反,请求 URI 始终包含前导/.

RewriteCond %{REQUEST_URI} ^/system/
RewriteRule ^(.*)$ index.php?/$1 [L]

虽然我不确定这是你想要做的,但这是有道理的。

后记

执行两条规则的更优雅的方法是将它们组合起来,例如:

RewriteCond %{REQUEST_URI} ^/(system|application)/
RewriteRule ^.* index.php?/$0 [L]
于 2014-03-04T10:47:18.423 回答
0

我遵循了此处答案中给出的建议,它似乎解决了这个问题。所以我认为问题在于改变选项指令。

我更改的文件部分httpd.conf现在如下所示:

<Directory />
 #AllowOverride none
 #Require all granted

 #Options FollowSymLinks
 Options Indexes FollowSymLinks Includes ExecCGI
 AllowOverride All
 Order deny,allow
 Allow from all
</Directory>

<Directory "c:/wamp/www/codeIgniter">
 #AllowOverride FileInfo AuthConfig
 #Options None
 Order allow,deny
 Allow from all

 AuthName "protected"
 AuthType SSPI
 SSPIAuth On
 SSPIAuthoritative On
 SSPIOmitDomain On 

 require valid-user
</Directory>

.htaccess我在我的文件中也对@TerryE 推荐的重写规则进行了更改。它们现在看起来像这样:

RewriteCond %{REQUEST_URI} ^/system/
RewriteRule ^(.*)$ index.php?/$1 [L]

RewriteCond %{REQUEST_URI} ^/application/
RewriteRule ^(.*)$ index.php?/$1 [L]
于 2014-03-04T13:25:37.173 回答