
在 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 Found
。https://example.com/error.php
另一方面,
https://example.com/protectedfolder
401
導致按預期定制(即由 error.php 產生) 。http://example.com/publicfolder
導致302
重定向到https://example.com/publicfolder
,然後301
永久重定向到https://example.com/publicfolder/
,最後(由於 DirectoryIndex 被停用)自訂403
錯誤。正如預期的那樣。- 此外,
http://example.com/nonexistent
也會導致 a302
tohttps://example.com/nonexistent
,然後導致customized404
,也如預期的那樣。 - 如果我停用該
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
由於您無條件重定向http
到https
(如果您希望每個人都能夠訪問您網站上的公共信息,那麼這是一個壞主意,順便說一句),那麼您應該為:80
和製作單獨的主機條目:443
,並將:80
條目指向類似空文件夾之類的東西 - 那麼您就不會讓 mod_auth 與 mod_rewrite 競爭。