【问题标题】:mod_rewrite no longer taking effect after applying sspi rules in server config在服务器配置中应用 sspi 规则后 mod_rewrite 不再生效
【发布时间】:2014-03-04 09:38:34
【问题描述】:

我正在 codeIgniter 中创建一个项目,使用 wamp 在本地开发 (Apache 2.4)。在整个项目中,我打开了 mod_rewrite 以删除默认情况下出现在 codeigniter 的 url 中的 index.php,方法是使用 codeigniter 项目目录中的 .htaccess 文件,即 C:\wamp\www\codeIgniter\.htaccess

我们需要在应用程序中进行 Windows 身份验证,因此在最终设法使 ldap 和 sspi 模块正常工作后,我将以下内容应用于 c:wamp\bin\apache\Apache2.4.4\conf\httpd.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>

我得到了这个配置here

httpd.conf 文件进行这些更改后,mod_rewrite 规则似乎不再适用,我必须在项目根目录后写入index.php 以获取要加载的网址。

在阅读了一些关于设置 httpd.conf 配置和 .htaccess 文件的 Apache 文档后,它建议如果可以访问服务器配置,则不应使用 .htaccess 文件:

一般来说,您应该只在没有 .htaccess 文件时使用 访问主服务器配置文件。例如,有一个 用户身份验证应始终在 .htaccess 文件,以及近年来的另一个误解,即 mod_rewrite 指令必须放在 .htaccess 文件中。这根本不是 案子。您可以将用户身份验证配置放在主 服务器配置,事实上,这是首选的方式 事物。同样,mod_rewrite 指令在许多情况下工作得更好 方面,在主服务器配置中。 Link

首先我想了解如何让我的 mod_rewrite 规则与我的 sspi 内容一起愉快地再次工作,其次如何将我的 mod_rewrite 规则从 .htaccess 移动到我的服务器配置文件 httpd.conf.

我已经尝试在服务器配置中切换AllowOverride All,就像上面提到的代码一样,但这似乎使一切都无法访问。我也尝试将Order allow,denyAllow from all 更改为Require all granted,但这似乎也会导致类似的问题。将我的 mod_rewrite 规则添加到服务器配置中也会导致所有内容都无法访问。任何指导将不胜感激!谢谢。

我的.htaccess如下:

<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

【问题讨论】:

    标签: apache .htaccess codeigniter mod-rewrite sspi


    【解决方案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]
    

    【讨论】:

    • 感谢您的回复和链接。如果我将AllowOverride None 更改为AllowOverride FileInfoAllowOverride All 或将其注释掉,我假设这需要任何以前的指令,例如&lt;Directory "c:/wamp/www"&gt;Allow from all 我收到以下403 错误:禁止你无权访问此服务器上的 /codeIgniter/。 我不太明白为什么会出现此错误页面,因为肯定设置了 AllowOverride AllFileInfo 我允许 .htaccess做我认为不会导致禁止错误的重写工作。
    • 我的意思是在前面的评论中说“或者把它注释掉,我认为这需要任何以前的指令,例如在&lt;Directory "c:/wamp/www"&gt; 中有AllowOverride All” - 不会让我编辑
    【解决方案2】:

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

    我更改的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]
    

    【讨论】:

    • 如我所说,阅读有关 AllowOverride 和 Options 的文档。您真的要允许用户列出您的 codeIgniter 文件夹的目录吗?我不知道你为什么需要 AllowOverride FileInfo AuthConfigOptions FollowSymLinks 以外的任何东西
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-08-08
    • 2012-12-24
    • 2011-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多