[Linux] [轉貼]Apache 網頁認證.htaccess 設定方法
g4 d2 x0 @1 `! K: f
% { r" N9 v R" H' q.htaccess
( k6 ]/ k8 c; v3 J0 K
.htaccess 可放在任何一網頁目錄內,當該目錄在 httpd.conf 設定不是 AllowOverride None 便可使用 .htaccess 的功能了,而設定作 AllowOverride All 更可使用 .htaccess 的所有功能了
7 ^8 H* S+ N1 |) R
.htaccess 可用作對該目錄及其子目錄進行取存控制,而不用修改 httpd.conf 及不用重新啟動伺服器軟件
2 Y; c* n( I9 [1 v$ a3 a' Y' k
--------------------------------------------------------------------------------
# |1 R8 e6 G) T% L3 G# _
httpd.conf 的設定
( C& V x$ O$ M) Y. L加上或取消註解 (#) ]定改成 AllowOverride All
3 C; W- s6 T, {9 b) A( [
然後儲存及重新啟動伺服器軟件
H# x7 @, T! u+ B$ d- {4 Z--------------------------------------------------------------------------------
' A: X2 K& d. w, P# x
.htaccess 的用法
# i: J: n6 c3 {先把 .htaccess 放在要用的目錄內 (Windows 系統內不可以直接把檔案改名作 .htaccess ,需用間接的方法,如在指令模式下 ren filename.txt .htaccess 或用 FTP 軟件修改該檔名)
5 e6 M' Z s6 `* Y: Q' s/ P( |
用法:
' _/ I0 \/ U% W( D. [1. 密碼保護
$ U, B" p& a5 n# B4 W6 X% x$ C2 x& J1 u.htaccess 內寫上:
* `7 O8 c) ?; b
AuthName "testing"
! b R9 R: D! X6 Q5 U5 C
AuthType Basic
5 B1 l4 g- Q! i8 J2 n& XAuthUserFile "C:/Apache/htdocs/testing/.htpasswd"
7 e3 j, T" q2 C2 K5 K5 s0 l3 A0 p& Qrequire valid-user
2 I8 h, w- j: O2 Y% W
AuthName 是當你進入密碼保護時,瀏覽器出現的說明
$ u, Z' @$ k% m# @
AuthUserFile "C:/Apache/htdocs/testing/.htpasswd" 是密碼檔案的位置
8 |" G+ a& {. e+ Q- `% `5 i9 {6 f
密碼檔案 (.htpasswd) 內寫上:
; G$ D6 ]/ K E) y% i/ f' iuser1:password1
" w, q, Y. b8 O5 Y2 ]) ?
user2:password2
3 ~) s- j& `& g但密碼不應直接寫上,即是如果有一使用者是 test ,密碼是 password ,先用 C:\Apache\bin 內的 htpasswd.exe 把密碼加密,用法如下] require group group1 ,因此只有 group1 的使用者可通過,即 john , peter 及 david
2 K# H% Z3 A# i. ]% F" O# ~* w
- U; P; ^. H( t+ N) P2. 自設錯誤報告文件
0 W8 Q4 M) u4 S
.htaccess 內寫上:
. B9 a% z7 j+ F, R' ]0 o
ErrorDocument 404 /404.html
2 y; Y: C3 E2 Z8 x2 I' Q2 t
如果找不到網頁時,便輸出 404.html 的內容,其他錯誤時做法亦然,如:
2 w6 t2 ]+ Y/ H" W0 LErrorDocument 404 "<html><body>找不到網頁</body></html>
4 A$ X# T N7 }7 W* y* b. V3 }
ErrorDocument 500 /500.html
& Y# x/ V; G. R5 V3 c1 V
7 m/ {( w6 V: ~* k" h. r+ ?0 I& P. x3. 自設首頁檔案
* k, Q& p4 M6 |. T
.htaccess 內寫上:
, A2 j: Q. z( w4 K0 B+ n2 `" IDirectoryIndex a.html
/ h7 g* A* \+ W: C首頁檔案便是 a.html 了
* f9 A! ^( s: x: K2 o/ G. ^, ~' t) e
4. 禁止讀取檔案
/ x/ V k: u' I$ o) h8 W, {
.htaccess 內寫上:
- S! a1 Q9 p1 B8 b9 v' D A; Y<Files secret.html>
- M( S( \ ` Worder allow,deny
: {+ g1 c+ I! N/ g% m
deny from all
! V' a& w' n# m! M/ [3 ^- e9 w6 M</Files>
. u0 y0 N8 j2 G4 Z' j2 sdeny from all 即所有人皆不能讀取 sercet.html
5 }7 \1 r2 T$ v
# u" Q6 Y" Z- u4 i" f5 X% _而 Apache 1.3 以後的版本,更可以用支援 regular expression 的 filesmatch
7 I) J' B: E6 m( F
.htaccess 內寫上:
: Z( a9 A/ Z& P; P( S! K+ f<filesmatch "\.jpg">
7 {/ V+ B8 s; W0 V, R6 }. _order allow,deny
_" e9 y1 ^ j) Zdeny from all
+ E+ C4 o. L4 @7 S/ j</filesmatch>
7 S2 ?9 Y+ e* ~即所有人皆不能讀取副檔名為 .jpg 的檔案
4 r9 E p3 H7 f5 N9 n
1 h. r) |! v3 f; l.htaccess 內寫上:
6 H% W9 \6 |1 [1 P
<Files secret.html>
0 t x8 ~* M+ `- corder allow,deny
* l) N( S$ E& e; |7 C+ I& ~, i
allow form all
: O3 R* |7 ?' p
deny form 202.202
. o+ z0 l& d- S; P
</Files>
- ]" q# K8 h/ [) V6 w% C即 ip 是以 202.202 開頭的不能讀取副檔名為 secret.html 的檔案
* p0 }7 Z6 {5 n# }" x7 ]+ c5 i
: D4 _ T( R- Z6 t2 _5 A# t9 p.htaccess 內寫上:
' y" u8 J/ v9 M5 D<Limit GET>
- w9 Q$ K; }" m( N4 B" Dorder deny,allow
+ |; B1 i) j& D; f' l- Ideny from all
/ o, Z3 ?+ M! a; pallow from 202.202 domain.com
& g$ ~) v) Y5 p% Q, |</Limit>
. X) o! Q& p3 B x( V
即只允許 ip 是以 202.202 為開頭或域名是 domain.com 的讀取此目錄內的任何內容
8 S2 D/ _& t9 d5 z# B: h4 s, e3 ~! A& `. P# B& m) k
.htaccess 內寫上:
- S0 p6 r! U( C H, o1 C" T8 e
AuthUserFile "C:/Apache/htdocs/testing/.htpasswd"
4 h7 s, X$ j, d0 r! E& M
<files sercet.html>
/ ?/ W9 B9 w0 }! }9 W, @6 ^. ^require user peter
9 o; M; {8 H6 \! p# G</files>
% s+ e; E' m* v# G7 t! d即只允許 .htpasswd 內列出的 peter 讀取 sercet.html
" e) f: B, H/ s' k& Z6 [0 Z
5 Q( z8 e g" {5. 重新導向文件
4 U7 u* S5 Z; O$ Y8 q.htaccess 內寫上:
* Q1 @$ a s3 b o+ |; n# A8 W2 `9 _+ aRedirect /old
http://url/new& W+ L0 v/ W+ g7 \# A
當用戶要求 /old 時,伺服器便把其重新導向至
http://url/new ,或在 .htaccess 內寫上:
) V3 P+ ]! {3 U/ A8 g. KRedirect permanent /old
http://url/new, V- e* e$ H4 P/ _. H
當用戶要求 /old 時,伺服器便叫瀏覽器把其永遠重新導向至
http://url/new: e- D, ~" w' z2 K6 } t( S& G
; u5 W! O$ p$ K2 Y8 ]! K( C5 t
6. 防止列出目綠內的檔案
) p" f3 R1 G8 c7 S$ B
.htaccess 內寫上:
& O* i; l& ~5 C2 i. Q2 D- ?Option -Indexes
' V$ k9 Z8 Q8 Q( j" L這便不能列出檔案目錄了
( n0 U1 s* b% W3 k" |
.htaccess 內寫上:
9 E7 R; O; ?! ~IndexIgnore *.zip *.txt
5 E" M' M- c3 f. _ `+ B
這便不列出檔案目錄內的 .zip 與 .txt 檔了
6 q: `5 Y# O* C; ]9 L$ `! H. v2 M4 V. ^$ y+ `! ~4 D
註: .htaccess 的大部分內容 (如:密碼保護) 可以寫回 httpd.conf 相應的目錄設定內( <Directory> )