我已经添加了一个目录到我的网站的公用文件夹,并在其中放置了下面的.htaccess文件:
AuthName "Restricted Area" AuthType Basic AuthUserFile /path/to/.htpasswd AuthGroupFile / Require valid-user
.htpasswd
文件存在并具有有效的内容。
在该站点的公用文件夹中,有一个带有以下重写规则的.htaccess文件:
RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . index.php
有了这个设置,我的/protected
文件夹不受保护。
/protected/.htaccess
文件,并且我的/protected
文件夹不受保护。 /protected
目录会导致密码提示,正如所料。 我修改了根.htaccess
重写规则,我应该阻止mod_rewrite
尝试重写请求到/protected
文件夹,如下所示:
RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_URI} !^/protected RewriteRule . index.php
但是这似乎没有影响:
/index.php
处理的,而不是像预期的那样提示authentication细节 /protected
文件夹不受保护 在我的受保护的文件夹中还有一个index.php
文件( /protected/index.php
),内容如下:
die('protected');
任何人都可以看到这种方法的问题,或与我的修改根.htaccess
重写规则?
Facepalm !
我的重写条件按预期工作,问题出在我的/protected/.htaccess
文件中:
对于基本身份validation,服务器会写入“401 Unauthorized”标头,然后根据预定义的path查找错误文档。 大多数情况下,错误文档不会存在于您要保护的目录中,因此请求会被引发404错误的重写引擎处理。
解决scheme是将以下内容添加到/protected/.htaccess
文件中:
ErrorDocument 401 "Unauthorized Access" RewriteEngine off ... Authorisation lines
从Apache URL重写和基本身份validation冲突 。