Apache中的系统错误与httpsredirect加上密码要求和错误文档的组合

在Apache 2虚拟主机中,我有以下configuration(在我的情况下,文档根目录的.htaccess (为简单起见,http:80和https:443也是如此)

 RewriteEngine On RewriteCond %{HTTPS} off RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} 

为了将任何http连接redirect到https而且,

 ErrorDocument 500 /error.php ErrorDocument 404 /error.php ErrorDocument 403 /error.php ErrorDocument 402 /error.php ErrorDocument 401 /error.php 

生成自定义错误消息。 第三个要素是一个受保护的子文件夹,需要进行身份validation(每个.htaccess在该文件夹中):

 AuthType Basic AuthName "Test" AuthUserFile /some/path/to/passwords Require user joe 

一切工作正常,除非有人试图检索http://example.com/protectedfolder事实上,会发生什么事情是,客户端获得302 Found答复redirect到https://example.com/error.php

另一方面,

  • 如预期的那样, https://example.com/protectedfolder会导致一个自定义(即,由error.php生成) 401
  • http://example.com/publicfolder导致302redirect到https://example.com/publicfolder ,然后301永久redirect到https://example.com/publicfolder/ ,最后(因为DirectoryIndex被禁用)a自定义403错误。 正如所料。
  • 而且, http://example.com/nonexistent也会导致302https://example.com/nonexistent ,然后按照预期的那样定制404
  • 如果我禁用了ErrorDocument 401configuration,则http://example.com/protectedfolder的查询会立即生成401 ,即不会redirect到https。

Apache error.log中没有特定的条目,但似乎是因为Auth需求在Rewrite之前被求值而出现问题,因此调用ErrorDocument,并且错误地仍然是http?

为了达到预期的效果,我需要更改什么内容,即http://example.com/protectedfolder会导致redirect到https://example.com/protectedfolder ,只有redirect的URL会导致(自定义的) 401

既然你做了无条件的从httphttpsredirect( 如果你希望每个人都能够访问你的网站上的公共信息 ,那么你应该为:80:443分别创build主机条目,指向:80条目像一个空文件夹 – 那么你不会有mod_auth与mod_rewrite竞争。