[root@chem html]# mkdir /var/www/html/protect

[root@chem html]# vi /var/www/html/protect/index.html

 

# 寫一個測試頁面,看的到代表保護網頁沒問題

# 連結為 http://www.chem.ccu.edu.tw/protect/index.html

<html>
<head><title>@test123123@</title></head>
<body>@test456456@
</body></html>
 

[root@chem html]# vi /etc/httpd/conf/httpd.conf

 

# 確認受保護的目錄可以使用.htaccess的檔案格式
AccessFileName .htaccess
 

# 確認底下這幾行有存在
<Files ~ "^\.ht">
Order allow,deny
Deny from all
</Files>
 

# 額外再加入這幾行
<Directory "/var/www/html/protect">
AllowOverride AuthConfig
Order allow,deny
Allow from all
</Directory>
 

[root@chem html]# service httpd restart

[root@chem html]# vi /var/www/html/protect/.htaccess

 

# 在出現要你輸入帳號與密碼的對話視窗中,出現的"訊息"

AuthName "Protect test by .htaccess"

# 認證的類型,預設為Basic
Authtype Basic

# 保護目錄所使用的帳號密碼設定檔
AuthUserFile /var/www/apache.passwd

# require user後面接可以使用的帳號

# require valid-user代表apache.passwd內所有的帳號皆可登入


require user test
 

# 新增使用者test

[root@chem html]# htpasswd -c /var/www/apache.passwd test
New password:(test)
Re-type new password:(test)
Adding password for user test
[root@chem html]# less /var/www/apache.passwd

 

test:nfxk1cH5sOqM6 (代表test使用者已被新增)

 

# 新增另一名使用者test1

[root@chem html]# htpasswd /var/www/apache.passwd test1
New password:
Re-type new password:
Adding password for user test1
 

# http://www.chem.ccu.edu.tw/protect/如果出現對話框要求輸入帳號密碼代表成功

# 但須注意只有test帳號可登入,因為.htaccess內require user後方的設定只有test