在開發新網站時,不想被別人看到開發的內容,或是網站只開放給特定人士用時,可以利用網頁認證來達到保護網站內容的功能,在這邊介紹如何利用 htaccess 來達到以上功能。
以下介紹在Linux系統利用 command line 方法
1. 在需保護的資料夾底下 (如果不知道就設定網頁最上層的都保護住) 打
# vim .htaccess
2. 編輯 .htaccess 的內容
輸入
AuthName "Admin"
AuthType Basic
AuthUserFile "/yourfolder/.passwd"
Require valid-user
註: AuthUserFile "/yourfolder/.passwd" 這邊請根據你的資料夾(yourfolder)放置密碼位置
你可以輸入 pwd 看現在的資料夾位置
如果你輸入 pwd 顯示 /home/melo/test 的話,你那一行就輸入
AuthUserFile "
/home/melo/test/.passwd"
3. 在.passwd資料夾底下 設定使用者及密碼
#htpasswd -c .passwd username
^^^^^^^
你的使用者名稱
ex: 你的使用者名稱是 melo 就輸入
#htpasswd -c .passwd melo
4. 要讓網頁成功透過 .htaccess 設定的話 就要更改 httpd.conf 內的參數設定
使用 root 編輯 httpd.conf
# vim /etc/httpd/conf/httpd.conf 輸入
<Directory "/yourfolder">
AllowOverride AuthConfig
Order allow,deny
Allow from all
</Directory>
接下來就輸入密碼 (ex:1234) 就設定完成了。
快來看看是否成功
成功的話 會像我一樣跑出這個要輸入帳號密碼
Admin 就是 AuthName 設定的名稱
紅色框框是你的網址
上述範例:
使用者名稱: melo
密碼: 1234
大功告成。
有問題歡迎來問喔,
謝謝指教。
Keyword
htaccess, htpasswd, password, web design, security, linux
感謝完整的分享
回覆刪除我有聽說也可以阻擋IP
請問大大知不知道怎麼做?
謝謝
你好~
回覆刪除只需要加入
Order Deny,Allow
Deny from xxx.xxx.xxx.xxx
xxx.xxx.xxx.xxx就是你要禁止的IP
就可以摟!!