Apache 中的系統錯誤,結合 https 重定向、密碼需求和錯誤文檔

Apache 中的系統錯誤,結合 https 重定向、密碼需求和錯誤文檔

在 Apache 2 虛擬主機中,我有以下配置(在我的例子中,在.htaccess文檔根目錄中(為簡單起見,http:80 和 https:443 相同)

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

為了將任何 http 連線重定向到 https 此外,

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

產生自訂錯誤訊息。第三個成分是需要身份驗證的受保護子資料夾(.htaccess該資料夾中的每個子資料夾):

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

一切正常,除非有人嘗試http://example.com/protectedfolder檢索302 Foundhttps://example.com/error.php

另一方面,

  • https://example.com/protectedfolder401導致按預期定制(即由 error.php 產生) 。
  • http://example.com/publicfolder導致302重定向到https://example.com/publicfolder,然後301永久重定向到https://example.com/publicfolder/,最後(由於 DirectoryIndex 被停用)自訂403錯誤。正如預期的那樣。
  • 此外, http://example.com/nonexistent也會導致 a 302to https://example.com/nonexistent,然後導致customized 404,也如預期的那樣。
  • 如果我停用該ErrorDocument 401配置,則會立即查詢http://example.com/protectedfolder原因401,即不重定向到 https。

Apache error.log 中沒有特定條目,但似乎出現問題是因為在重寫之前評估了 Auth 要求,從而調用了 ErrorDocument,並且錯誤地仍然是 http?

我需要更改什麼才能達到所需的效果,即http://example.com/protectedfolder導致重定向到https://example.com/protectedfolder並且只有重定向的 URL 才會導致 (自訂) 401

答案1

由於您無條件重定向httphttps(如果您希望每個人都能夠訪問您網站上的公共信息,那麼這是一個壞主意,順便說一句),那麼您應該為:80和製作單獨的主機條目:443,並將:80條目指向類似空文件夾之類的東西 - 那麼您就不會讓 mod_auth 與 mod_rewrite 競爭。

相關內容