[Linux] [轉貼]Apache 網頁認證.htaccess 設定方法
3 ~ m# ]" d5 R% h n
( v. N9 N# s/ n& h' C1 d.htaccess
/ B* |- Y0 Q) q8 \5 Q.htaccess 可放在任何一網頁目錄內,當該目錄在 httpd.conf 設定不是 AllowOverride None 便可使用 .htaccess 的功能了,而設定作 AllowOverride All 更可使用 .htaccess 的所有功能了
9 r. O% E* O4 a6 c1 s.htaccess 可用作對該目錄及其子目錄進行取存控制,而不用修改 httpd.conf 及不用重新啟動伺服器軟件
" g& z% S# R( l, H$ y* [--------------------------------------------------------------------------------
! n: O M+ q3 O
httpd.conf 的設定
) q3 B8 O" [# O9 e, j) @
加上或取消註解 (#) ]定改成 AllowOverride All
/ r/ \, n: E0 [" a
然後儲存及重新啟動伺服器軟件
5 j# u& @' ~; ?
--------------------------------------------------------------------------------
$ d4 h. n* Q( y3 T.htaccess 的用法
+ ]( [5 y7 @! e7 v) G0 c先把 .htaccess 放在要用的目錄內 (Windows 系統內不可以直接把檔案改名作 .htaccess ,需用間接的方法,如在指令模式下 ren filename.txt .htaccess 或用 FTP 軟件修改該檔名)
4 c; ], ], @+ M+ R
用法:
4 M1 {* q8 }% R8 |" @1. 密碼保護
U# c# h- x9 j% n& l G.htaccess 內寫上:
; ]& u% N' D5 n/ u3 WAuthName "testing"
4 P1 v% ?, g! }AuthType Basic
4 c( j( C4 r. `4 a7 J/ ^AuthUserFile "C:/Apache/htdocs/testing/.htpasswd"
5 @3 W, W+ I" G* l- ^
require valid-user
$ D4 K4 S+ F: O6 s% y0 c
AuthName 是當你進入密碼保護時,瀏覽器出現的說明
p6 a5 |. @& ~& U4 e! [0 d( Q, X* PAuthUserFile "C:/Apache/htdocs/testing/.htpasswd" 是密碼檔案的位置
3 q- ?+ C2 a: ~5 x' V密碼檔案 (.htpasswd) 內寫上:
) i4 x) a3 ]* @& p$ B. W
user1:password1
7 `. L" Z0 O M
user2:password2
+ Q- v0 n4 D; r/ g% ^2 i
但密碼不應直接寫上,即是如果有一使用者是 test ,密碼是 password ,先用 C:\Apache\bin 內的 htpasswd.exe 把密碼加密,用法如下] require group group1 ,因此只有 group1 的使用者可通過,即 john , peter 及 david
$ ~& X o9 r9 G
+ r. z6 L, v% g K J2. 自設錯誤報告文件
+ O: k5 C% }% n# Z, p.htaccess 內寫上:
+ z3 [# X1 Z9 K) S9 UErrorDocument 404 /404.html
2 b* h5 E+ G% r" @7 i
如果找不到網頁時,便輸出 404.html 的內容,其他錯誤時做法亦然,如:
, f9 p# B( P4 I6 o+ W8 \: X% S4 AErrorDocument 404 "<html><body>找不到網頁</body></html>
2 N2 I) M& u9 @$ q. ^. M4 _ErrorDocument 500 /500.html
. N8 L# ?; n i% a9 u& m5 V% K
, }4 V- G; o) T2 G, M* u3. 自設首頁檔案
1 e4 _& X. j' \3 q: M' r
.htaccess 內寫上:
, z* ?5 Z0 D7 t9 a1 E) A. G$ CDirectoryIndex a.html
, p. n+ A2 s( z' f# J
首頁檔案便是 a.html 了
: e9 e- U3 O. ?! r* a, }5 P9 W
# O/ ~5 |" z+ c" _1 V3 S! V4. 禁止讀取檔案
' k4 C2 B* _+ `7 [3 T4 ^
.htaccess 內寫上:
* E. r0 a1 d8 g0 q5 {8 _
<Files secret.html>
% v9 E( F! a7 K% I4 h* g
order allow,deny
9 p& E' y# W4 q2 Pdeny from all
: X& l+ Z6 N/ n1 n) e2 Y! v
</Files>
' k) x/ Q( a% T5 w& M. odeny from all 即所有人皆不能讀取 sercet.html
$ F. V+ e; x7 ?# _$ _0 R! L
1 I5 ]6 S6 m% i' x$ m而 Apache 1.3 以後的版本,更可以用支援 regular expression 的 filesmatch
( Z3 y2 z/ z) ^7 O& R.htaccess 內寫上:
; f+ m& l' A/ x<filesmatch "\.jpg">
3 n1 x; E* O/ p! sorder allow,deny
% } f+ {( I) L4 G
deny from all
) J0 U# f2 B, z7 b0 |0 g0 }</filesmatch>
) m' A* a. z2 w9 Y& s# S即所有人皆不能讀取副檔名為 .jpg 的檔案
( u/ K4 b# D, X
7 i; u5 G: `- b( p* o.htaccess 內寫上:
/ N/ S2 c" u6 ^
<Files secret.html>
; v. g3 v+ m6 u8 \
order allow,deny
" {2 @- o; P% Y) d3 ?- ]
allow form all
( P X6 K1 b+ j8 ?9 Qdeny form 202.202
' T& n1 Q8 a s, S/ |6 z; z4 t</Files>
: e. C) t& R: x# @6 Q+ t: [, m即 ip 是以 202.202 開頭的不能讀取副檔名為 secret.html 的檔案
' [1 n( T4 d* t9 I; c4 [
! q0 A J6 M: Y3 @" }: I$ R
.htaccess 內寫上:
8 x: A; s8 z0 |0 D* n$ ? w" Y<Limit GET>
. D- G5 d$ A9 y, B. S3 R" Eorder deny,allow
N; R. Y% P7 x. H- odeny from all
6 C8 ]- \1 S0 o* u& Wallow from 202.202 domain.com
5 b& `+ x+ o# F2 O
</Limit>
6 O* r% n3 |) }6 a即只允許 ip 是以 202.202 為開頭或域名是 domain.com 的讀取此目錄內的任何內容
; O2 P5 \, B" e ~5 G9 c: A) h
% @3 @/ h D& t# @2 [.htaccess 內寫上:
) ^7 O, j0 z: j% V! z$ k* E
AuthUserFile "C:/Apache/htdocs/testing/.htpasswd"
- F8 N, U0 {2 ^; @# ]6 K
<files sercet.html>
. c `8 i2 w! p3 |" ], s" srequire user peter
8 j& A1 C- Q; y; B7 N</files>
1 R7 L9 J( J6 X( h u3 ~即只允許 .htpasswd 內列出的 peter 讀取 sercet.html
* f% D' }$ E* H8 R! B. F5 M
, {$ J4 g3 A! i0 J9 }( U* K
5. 重新導向文件
% h: ^, A1 Q4 [8 T, v
.htaccess 內寫上:
3 V9 c: f8 k3 z* ^7 \
Redirect /old
http://url/new
K) s( v) t. f# @+ Q當用戶要求 /old 時,伺服器便把其重新導向至
http://url/new ,或在 .htaccess 內寫上:
?# L; {7 ^$ x
Redirect permanent /old
http://url/new
5 s4 H/ J1 `( i當用戶要求 /old 時,伺服器便叫瀏覽器把其永遠重新導向至
http://url/new
; c8 c( T% Z! J' T1 P& }: l; R( z9 Y9 z) g7 v' b4 ^4 S
6. 防止列出目綠內的檔案
5 b% F( A* u6 D. Z Q R.htaccess 內寫上:
2 [& h- W1 t$ ~ ^$ vOption -Indexes
7 [& O! U; ~. E5 X
這便不能列出檔案目錄了
* b4 i" q, {" x2 V: j! W.htaccess 內寫上:
7 D5 D; g: u# I6 S( X }
IndexIgnore *.zip *.txt
" u+ v6 Z) P% h* ~% X+ Z) t
這便不列出檔案目錄內的 .zip 與 .txt 檔了
! G: f9 s4 ~) i% k! H
L( U# Z, t* u1 h' K
註: .htaccess 的大部分內容 (如:密碼保護) 可以寫回 httpd.conf 相應的目錄設定內( <Directory> )