[英]Protect folder with password fails on localhost (under Apache and Windows)
我試圖在 Windows 機器上的 Apache 網絡服務器下的本地主機上使用用戶名和密碼保護文件夾不被直接訪問。 根據這篇文章,我在 phpinfo 結果中查找了DOCUMENT_ROOT
值,發現以下內容: C:/Program Files/Apache Software Foundation/Apache2.2/htdocs
。
由於我在htdocs
文件夾稱為protected
,因此我創建了以下 htaccess 文件:
AuthType Basic
AuthName "restricted area"
AuthUserFile C:/Program Files/Apache Software Foundation/Apache2.2/htdocs/protected/.htpasswd
require valid-user
和以下.htpasswd(用戶:測試,密碼:測試)
test:teH0wLIpW0gyQ
但是當我嘗試訪問該文件夾時,我收到“內部服務器錯誤”。 查看 Apache 錯誤日志,我發現以下行:
[Thu Mar 13 22:00:15 2014] [alert] [client 127.0.0.1] C:/Program Files/Apache Software Foundation/Apache2.2/htdocs/protected/.htaccess: AuthUserFile takes 1-2 arguments, text file containing user IDs and passwords
我錯過了什么? 我似乎與 .htpasswd 路徑中的空格無關,但與 .htpasswd sintax 相關。 我想構建可以在本地主機和在線上使用的東西。
更新
將 htpasswd 路徑包含在引號中可消除錯誤
AuthUserFile "C:/Program Files/Apache Software Foundation/Apache2.2/htdocs/protected/.htpasswd"
但不幸的是不起作用(如果我在需要時輸入 user:test, password:test 我收到密碼不匹配錯誤)。 請參閱以下 apache 日志:
[Thu Mar 13 22:26:43 2014] [error] [client 127.0.0.1] user test: authentication failure for "/protected/": Password Mismatch
有任何想法嗎?
解決了! 這里是在 windows 下配置 Apache 以使用密碼保護文件夾的步驟(在本例中,我使用“protected”作為文件夾名稱,使用“htdocs”作為位置,但您可以根據需要更改它們):
http://localhost/protected/
.htaccess
AuthType Basic
AuthName "restricted area"
AuthUserFile "C:/Program Files/Apache Software Foundation/Apache2.2/htdocs/protected/.htpasswd"
require valid-user
希望它可以幫助某人。
如果您在 Windows 中記得確保您可以看到文件擴展名。 在創建我的 htaccess 文件等時,這把我搞砸了……正在將它們創建為文本文件。
因此,只需確保另存為所有文件即可終止 .txt 擴展名。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.