[Linux] [轉貼]Apache 網頁認證.htaccess 設定方法
6 S9 |% N9 M8 q" }+ s$ O) H! \
! I. X: ]5 Y( J2 I* z$ v: K
.htaccess
2 F) `% _- b% a* |% d.htaccess 可放在任何一網頁目錄內,當該目錄在 httpd.conf 設定不是 AllowOverride None 便可使用 .htaccess 的功能了,而設定作 AllowOverride All 更可使用 .htaccess 的所有功能了
, Z1 p* [" P7 X7 N/ p
.htaccess 可用作對該目錄及其子目錄進行取存控制,而不用修改 httpd.conf 及不用重新啟動伺服器軟件
/ e/ A" u7 W9 Z& s2 O( ~: j
--------------------------------------------------------------------------------
8 ^# r3 y* g& @& B+ T* G- hhttpd.conf 的設定
* j4 b- f: O+ A6 J; [; g
加上或取消註解 (#) ]定改成 AllowOverride All
9 ^: A% J! f$ d( j然後儲存及重新啟動伺服器軟件
6 | d: n3 @' b2 Y4 T1 V
--------------------------------------------------------------------------------
0 s' W5 m! X5 U
.htaccess 的用法
. l7 O4 Q- B& e$ @/ g
先把 .htaccess 放在要用的目錄內 (Windows 系統內不可以直接把檔案改名作 .htaccess ,需用間接的方法,如在指令模式下 ren filename.txt .htaccess 或用 FTP 軟件修改該檔名)
% W r0 B3 F% q- p/ ]
用法:
- b7 X. t( t1 I% B! l9 ]" _
1. 密碼保護
/ S5 Y9 u. q- E+ D' j( U6 A.htaccess 內寫上:
! C0 J' J5 a) t9 E5 j; [8 G
AuthName "testing"
* A5 @. @9 f: c' p
AuthType Basic
) v- r8 B3 y3 y; m
AuthUserFile "C:/Apache/htdocs/testing/.htpasswd"
! E6 {( g- p# [; n4 Srequire valid-user
: C% ]( K+ ^. E' {
AuthName 是當你進入密碼保護時,瀏覽器出現的說明
' l+ i8 b7 d4 x2 W: j' c7 Y3 [
AuthUserFile "C:/Apache/htdocs/testing/.htpasswd" 是密碼檔案的位置
$ s. E4 H0 @% `
密碼檔案 (.htpasswd) 內寫上:
2 X8 Q3 D4 `% o% euser1:password1
d$ V( S9 y+ n( P/ b1 }user2:password2
0 G* e1 K5 e v4 k7 e& }4 @
但密碼不應直接寫上,即是如果有一使用者是 test ,密碼是 password ,先用 C:\Apache\bin 內的 htpasswd.exe 把密碼加密,用法如下] require group group1 ,因此只有 group1 的使用者可通過,即 john , peter 及 david
/ M. b& Z/ W, V1 S
' a4 ]* H1 ~9 N1 } T$ R8 R) N2. 自設錯誤報告文件
; k$ Q+ M) E; c5 c.htaccess 內寫上:
, Z0 y u% M! m4 w+ I+ j' Q6 \8 `# o
ErrorDocument 404 /404.html
/ P$ a4 e' N3 B6 X" b$ b3 |
如果找不到網頁時,便輸出 404.html 的內容,其他錯誤時做法亦然,如:
@( u( h' r4 Z- o- P+ z
ErrorDocument 404 "<html><body>找不到網頁</body></html>
0 T" l0 B# i; f3 C* d( O
ErrorDocument 500 /500.html
3 x L+ i- W% U; R! C6 t# }3 m
2 U _& d2 f1 C2 r3 O/ A& P3. 自設首頁檔案
3 F4 E: @. p! Z9 Q( S' A.htaccess 內寫上:
' S9 @4 a" c2 i+ }+ g
DirectoryIndex a.html
: h$ a1 j" j0 B/ V2 o首頁檔案便是 a.html 了
$ G( U# ~& B+ e9 _2 j
7 N' A6 G) `, T) Q$ R9 N7 T- q; W4. 禁止讀取檔案
3 T4 k/ A( o" r0 p( S" Y% f
.htaccess 內寫上:
& d) f6 M. c# j+ `7 B* A8 U
<Files secret.html>
+ p3 Y J5 ]! R$ i( O$ l) Z
order allow,deny
+ `) {0 t R& ydeny from all
' k$ T5 Y5 [% { t" S1 v</Files>
: j- H3 k& s$ f3 W% S- S# d
deny from all 即所有人皆不能讀取 sercet.html
4 w) y4 f6 o" ^) E8 O
. \$ O7 S; |2 G6 l而 Apache 1.3 以後的版本,更可以用支援 regular expression 的 filesmatch
8 P0 g( U7 C" D7 l( U; Y$ X
.htaccess 內寫上:
- [) H! X/ f& }$ w, t' L9 d- G<filesmatch "\.jpg">
& d* s* U8 o/ I2 {) \% ~+ Z
order allow,deny
4 A1 ^( F9 J, s6 C) w4 X" l8 k# K, x
deny from all
8 s( _1 F; J. `% B- }( U* s</filesmatch>
, t; T) y" q. a; s0 M* n& D即所有人皆不能讀取副檔名為 .jpg 的檔案
) o9 a: D3 o- F) R
# z+ `; x [3 H$ D4 ]3 q* q/ B0 Q
.htaccess 內寫上:
, O) s) c0 x+ O1 x<Files secret.html>
1 F& M" K3 i5 L% B& r2 q- F3 sorder allow,deny
& D7 N4 [* B: t
allow form all
* X4 e% \" J8 }' ideny form 202.202
7 _/ n2 |3 [6 \7 l! x8 L
</Files>
9 l0 g0 P) y) u5 g( {$ t. f
即 ip 是以 202.202 開頭的不能讀取副檔名為 secret.html 的檔案
. h# @ N3 Q7 B% O" l0 a* O! R# O+ d b( n/ I
.htaccess 內寫上:
1 R' m8 W1 x: Y' V7 _1 @<Limit GET>
$ g; Y3 _8 D3 b
order deny,allow
" w: h3 h9 s- j- C8 Ydeny from all
, _1 L* |, p6 Q* a
allow from 202.202 domain.com
/ j$ g( a& M1 E5 r+ y
</Limit>
0 K# X$ ^( N8 R% V, e即只允許 ip 是以 202.202 為開頭或域名是 domain.com 的讀取此目錄內的任何內容
. m; s' [$ ^9 o' G6 _0 r8 O8 u0 t) _6 k( {+ d7 }1 c
.htaccess 內寫上:
# ]4 X$ B! b ] g6 T7 k
AuthUserFile "C:/Apache/htdocs/testing/.htpasswd"
5 ~3 P+ L$ O: w9 Q! v<files sercet.html>
3 p6 [' ^; s: ]# ]* Y5 erequire user peter
# l! @) B9 V( m% q* ?/ G
</files>
5 `' l/ p; y( \2 l' J5 X
即只允許 .htpasswd 內列出的 peter 讀取 sercet.html
& b% b, l7 w' b6 L
' e. f( r$ l! ], b0 g
5. 重新導向文件
6 I. p9 R' y! r3 e3 }, J6 d$ r% M.htaccess 內寫上:
$ b8 }2 [6 d% [& ZRedirect /old
http://url/new
+ j& b1 W9 [* H0 D( [( W/ E4 |當用戶要求 /old 時,伺服器便把其重新導向至
http://url/new ,或在 .htaccess 內寫上:
) v/ E* i" Q/ C8 o+ A, b* YRedirect permanent /old
http://url/new' X# F5 D4 L: t) j( n7 G6 [
當用戶要求 /old 時,伺服器便叫瀏覽器把其永遠重新導向至
http://url/new
1 v6 z; e2 p7 b- d3 `6 m9 X9 B8 ^8 W9 ~: o- T! S0 ^) D2 j
6. 防止列出目綠內的檔案
) A7 J0 B) f3 T0 R2 W" V.htaccess 內寫上:
) L; P, y7 c. O3 k9 X& F- U: eOption -Indexes
7 k, }9 Z) w1 V% C9 @, c這便不能列出檔案目錄了
# [' Y9 v" B" a/ s, T.htaccess 內寫上:
5 a$ j3 G1 k& O' i7 M
IndexIgnore *.zip *.txt
5 d0 ^6 O9 T4 i" F {" l$ A1 L
這便不列出檔案目錄內的 .zip 與 .txt 檔了
3 E* N; y) y" j) y Q4 }+ z8 v
9 z. b+ Z- @: J6 [
註: .htaccess 的大部分內容 (如:密碼保護) 可以寫回 httpd.conf 相應的目錄設定內( <Directory> )