這是針對某一特定目錄裡的內容做保護,是一個快速而容易實現保護網頁內容的作法。
# vi /etc/httpd/conf/httpd.conf
// 確定底下這幾行是存在的!
AccessFileName .htaccess
Order allow,deny
Deny from all
// 在某個不受影響的地方,也許是在最後面,加入:
// 受保護的目錄 /var/www/html/mydoc,這個目錄要自行建立
AllowOverride AuthConfig
Order allow,deny
Allow from all
// 產生 .htaccess 檔
# cd /var/www/html/mydoc
# vi .htaccess
// 加入底下這幾行
AuthName “Protect test by .htaccess”
Authtype Basic
AuthUserFile /var/www/apache.passwd
require user test
// AuthName:輸入密碼的提示字元
// Authtype:認證類型,預設 Basic
// AuthUserFile:帳號密碼設定檔,這個檔案絕對不要放在網頁可以存取到的地方喔
// require: apache.passwd 可使用的帳號,如果要讓該密碼檔內的使用者都能夠登入,就改成『require valid-user』即可!
// 接下來要產生 apache.passwd 帳號密碼設定檔
# htpasswd -c /var/www/apache.passwd test
// 參數 -c :建立後面的密碼檔案。如果該檔案已經存在,則原本的資料會被刪除!所以如果只是要新增使用者(檔案已存在時),不必加上 -c 的參數!
New password:
Re-type new password:
Adding password for user test
// 重新啟動 apache
# service httpd restart