引用 | 編輯
aa
2002-12-12 11:49 |
樓主
▼ |
||
x0
.htaccess.htaccess 可放在任何一網頁目錄內,當該目錄在 httpd.conf 設定不是 AllowOverride None 便可使用 .htaccess 的功能了,而設定作 AllowOverride All 更可使用 .htaccess 的所有功能了 .htaccess 可用作對該目錄及其子目錄進行取存控制,而不用修改 httpd.conf 及不用重新啟動伺服器軟件 -------------------------------------------------------------------------------- httpd.conf 的設定 加上或取消註解 (#) ]定改成 AllowOverride All 然後儲存及重新啟動伺服器軟件 -------------------------------------------------------------------------------- .htaccess 的用法 先把 .htaccess 放在要用的目錄內 (Windows 系統內不可以直接把檔案改名作 .htaccess ,需用間接的方法,如在指令模式下 ren filename.txt .htaccess 或用 FTP 軟件修改該檔名) 用法: 1. 密碼保護 .htaccess 內寫上: AuthName "testing" AuthType Basic AuthUserFile "C:/Apache/htdocs/testing/.htpasswd" require valid-user AuthName 是當你進入密碼保護時,瀏覽器出現的說明 AuthUserFile "C:/Apache/htdocs/testing/.htpasswd" 是密碼檔案的位置 密碼檔案 (.htpasswd) 內寫上: user1:password1 user2:password2 但密碼不應直接寫上,即是如果有一使用者是 test ,密碼是 password ,先用 C:\Apache\bin 內的 htpasswd.exe 把密碼加密,用法如下] require group group1 ,因此只有 group1 的使用者可通過,即 john , peter 及 david 2. 自設錯誤報告文件 .htaccess 內寫上: ErrorDocument 404 /404.html 如果找不到網頁時,便輸出 404.html 的內容,其他錯誤時做法亦然,如: ErrorDocument 404 "<html><body>找不到網頁</body></html> ErrorDocument 500 /500.html 3. 自設首頁檔案 .htaccess 內寫上: DirectoryIndex a.html 首頁檔案便是 a.html 了 4. 禁止讀取檔案 .htaccess 內寫上: <Files secret.html> order allow,deny deny from all </Files> deny from all 即所有人皆不能讀取 sercet.html 而 Apache 1.3 以後的版本,更可以用支援 regular expression 的 filesmatch .htaccess 內寫上: <filesmatch "\.jpg"> order allow,deny deny from all </filesmatch> 即所有人皆不能讀取副檔名為 .jpg 的檔案 .htaccess 內寫上: <Files secret.html> order allow,deny allow form all deny form 202.202 </Files> 即 ip 是以 202.202 開頭的不能讀取副檔名為 secret.html 的檔案 .htaccess 內寫上: <Limit GET> order deny,allow deny from all allow from 202.202 domain.com </Limit> 即只允許 ip 是以 202.202 為開頭或域名是 domain.com 的讀取此目錄內的任何內容 .htaccess 內寫上: AuthUserFile "C:/Apache/htdocs/testing/.htpasswd" <files sercet.html> require user peter </files> 即只允許 .htpasswd 內列出的 peter 讀取 sercet.html 5. 重新導向文件 .htaccess 內寫上: Redirect /old http://url/new 當用戶要求 /old 時,伺服器便把其重新導向至 http://url/new ,或在 .htaccess 內寫上: Redirect permanent /old http://url/new 當用戶要求 /old 時,伺服器便叫瀏覽器把其永遠重新導向至 http://url/new 6. 防止列出目綠內的檔案 .htaccess 內寫上: Option -Indexes 這便不能列出檔案目錄了 .htaccess 內寫上: IndexIgnore *.zip *.txt 這便不列出檔案目錄內的 .zip 與 .txt 檔了 註: .htaccess 的大部分內容 (如:密碼保護) 可以寫回 httpd.conf 相應的目錄設定內( <Directory> ) x0
|
引用 | 編輯
aa
2002-12-12 11:50 |
1樓
▲ ▼ |
首先在你要保護的目錄下建立一個名為.htaccess的檔案,內容大致如下:
# 隨便取個名字吧,它會出現在瀏覽器的蹦出視窗中。 AuthName 阿達的祕密基地 # 編碼方式,目前只支援Basic一種而已。 AuthType Basic # 密碼檔的絕對路徑,可用htpasswd指令建立。 AuthUserFile /home/.../filename # 若下面有指定 require group,則需要group檔的絕對路徑。 AuthGroupFile /home/.../filename <Limit GET POST> require user user1 user2 ... require group group1 group2 ... </Limit> 它的密碼認證方式有二種,一種是指定users,也就是只有指定的username才有資格進入這個受保護的目錄。另一種是則指定group,凡屬於指定的group的users才有資格進入這個受保護的目錄,而group中到底有哪些users是記錄在AuthGroupFile中所指定的檔案,users的密碼檔則是指定在AuthUserFile中。且讓我們來看看這兩個檔的格式: group檔: group1:user1 user2 ... group2:user3 user4 ... ... passwd檔: user1:password user2:password ... 密碼檔裡的密碼是編碼過的,你可以使用htpasswd指令來建立密碼檔或新增users。而且由於密碼是用crypt函數編碼的,所以你也可以自己寫個CGI程式來新增users。 另外還有個安全問題要提醒你:密碼檔不要放在www文件的樹目錄中,最好是放在www目錄外面,以免密碼檔被不法之徒偷去猜密碼。 OK!!就這麼簡單,現在你可以試試看成功了沒... x0 |
引用 | 編輯
aa
2002-12-12 11:53 |
2樓
▲ ▼ |
一、建立.htaccess檔案
首先login cyber.cs.ntou.edu.tw,在你要保護的目錄下建立一個名為.htaccess 內容為 複製程式 # 隨便取個名字吧,它會出?#123;在瀏覽器的蹦出視窗中。 AuthName 國立臺灣海洋大學資訊系通訊錄查詢 # 編碼方式,目前只支援Basic一種而已。 AuthType Basic # 密碼檔的絕對路徑,可用htpasswd指令建立 # (.htpasswd檔可在ind.ntou.edu.tw上建好再傳回cyber.cs.ntou.edu.tw)。 AuthUserFile /home/under/b86204/.htpasswd #^^^^^ #自己的帳戶名 # 若下面有指定 require group,則需要group檔的絕對路徑。 # AuthGroupFile /home/under/b86204/filename <Limit GET POST> require user franklin require user beef require user david require user huangant #^^^^^^^^^ #可?#91;看你的Project的username #require group my-users group2 ... </Limit> AuthGroupFile 檔案內容範例]franklin:Z4vr73LqdneRA beef:RVN7zpfopMpiY davidɈmUlggYxqB4KQ huangant:DtAmaQ49hJuWk [/code] ^^^^^^^^^^^^^^^^^^^^ usernamegcrypt編碼過的密碼 當然最簡單的方法,就是 telnet ind.ntou.edu.tw這台主機, 利用htpasswd程式建立.htpasswd,再傳回cyber.cs.ntou.edu.tw。 方法如下所示] % telnet ind.ntou.edu.tw (login的過?#123;略) % htpasswd -c htpasswd.txt franklin Adding password for franklin New password: Re-type new password: % htpasswd htpasswd.txtbeef Adding user beef New password: Re-type new password: (其餘的username 之密碼建立,如同"htpasswd htpasswd.txt beef"一般, 以此類推。如果 htpasswd ?#123;式有任何疑問,不妨用"htpasswd -h"查查) %ftp cyber.cs.ntou.edu.tw (ftp過?#123;略) ftp>mput htpasswd.txt ftp>bye %telnet cyber.cs.ntou.edu.tw (login過?#123;略) % mv htpasswd.txt .htpasswd[/code] 三、要如何在Homepage或CGI辨識那一個user在瀏覽此一加密的目錄? 不外乎要使用環境變數,但是需要一些前置作業]% cp /usr/local/etc/httpd/cgi-bin/w3-msql ~/www/project ^^^^^^^^^^^^^ Project location % mv ~/www/project/w3-msql ~/www/project/w3-msql.cgi[/code] 在你呼叫含有w3-msql語法的html檔時,要用 複製程式 "http://cyber.cs.ntou.edu.tw/~b86204/project/w3-msql.cgi/~b86204/project/project.html" ^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ your cgi your project file 如此一來,就可以在w3-msql中,用$REMOTE_USER得知目前的user是誰了。 四、如何限制某些 domain 的存取 複製程式 AuthName Example AuthType Basic AuthUserFile /home/under/b86204/.htpasswd AuthGroupFile /home/under/b86204/filename <Limit GET> order deny, allow deny from all allow from .ntou.edu.tw </Limit> x0 |