[Linux] [轉貼]Apache 網頁認證.htaccess 設定方法
* u* } H, r' m, n+ @( u0 _& g5 \" s+ d
.htaccess
" I7 H; C6 ?; h! Y1 L, `
.htaccess 可放在任何一網頁目錄內,當該目錄在 httpd.conf 設定不是 AllowOverride None 便可使用 .htaccess 的功能了,而設定作 AllowOverride All 更可使用 .htaccess 的所有功能了
9 ?& h: u( x% q7 r) p2 h( f
.htaccess 可用作對該目錄及其子目錄進行取存控制,而不用修改 httpd.conf 及不用重新啟動伺服器軟件
3 I" T+ S1 j" w
--------------------------------------------------------------------------------
7 m( L' ~8 c7 W/ dhttpd.conf 的設定
( N/ V6 H8 `! c. a+ K5 M* d
加上或取消註解 (#) ]定改成 AllowOverride All
6 e e" G- J: T E* {+ }
然後儲存及重新啟動伺服器軟件
8 C0 y* ?2 m2 [
--------------------------------------------------------------------------------
$ s- E# a4 ]: C.htaccess 的用法
) V P# K" A0 z+ ] }+ t' s/ L+ O先把 .htaccess 放在要用的目錄內 (Windows 系統內不可以直接把檔案改名作 .htaccess ,需用間接的方法,如在指令模式下 ren filename.txt .htaccess 或用 FTP 軟件修改該檔名)
9 N( o& V% ^+ _+ b6 `/ v
用法:
9 ]" d, h/ w1 G% s4 F* J# _- E+ y
1. 密碼保護
0 Z8 Y; m. q3 x0 a6 C6 O: H e+ o$ ^2 T.htaccess 內寫上:
% X( Y% `8 Q/ ?! w% bAuthName "testing"
5 j# u5 H+ v/ R7 s
AuthType Basic
2 W* y7 \* E8 |" G9 l0 iAuthUserFile "C:/Apache/htdocs/testing/.htpasswd"
% j5 W9 s) T2 _- s+ e9 |
require valid-user
0 Y/ y9 k4 `0 R3 |* e5 u/ uAuthName 是當你進入密碼保護時,瀏覽器出現的說明
+ V, G. X' z3 E
AuthUserFile "C:/Apache/htdocs/testing/.htpasswd" 是密碼檔案的位置
, S; Y2 V* Z: \4 Y) b密碼檔案 (.htpasswd) 內寫上:
" Y$ x5 T9 T7 |' q {user1:password1
; Q1 Y5 ]3 C( t) X( G, tuser2:password2
7 r) m; o2 W; C; `
但密碼不應直接寫上,即是如果有一使用者是 test ,密碼是 password ,先用 C:\Apache\bin 內的 htpasswd.exe 把密碼加密,用法如下] require group group1 ,因此只有 group1 的使用者可通過,即 john , peter 及 david
3 S8 N$ D5 j+ e3 \
4 B5 L5 {; P0 p/ u9 k2. 自設錯誤報告文件
( f! [8 `- }+ d6 V2 ^) h7 n
.htaccess 內寫上:
$ r) S& i& u* e4 @1 D# Y& G0 }ErrorDocument 404 /404.html
; N2 N% Y1 O$ w4 O; Z( B
如果找不到網頁時,便輸出 404.html 的內容,其他錯誤時做法亦然,如:
\' V9 a5 h# ?) ^& `' Y j! cErrorDocument 404 "<html><body>找不到網頁</body></html>
4 R$ x; p& [) {# CErrorDocument 500 /500.html
% ]6 c! [) N/ U9 L0 \
* u' o9 Y- Q o5 T1 T) u6 d! J3. 自設首頁檔案
5 i- v7 v- |8 C3 }5 L.htaccess 內寫上:
/ a6 w1 B6 E9 K0 {; T( n& T$ pDirectoryIndex a.html
/ e5 [3 d3 ]# u& d2 R% K
首頁檔案便是 a.html 了
! J& T0 {" o6 |5 H& M6 U2 F
1 O8 K0 @3 c' X" b+ ^; O
4. 禁止讀取檔案
, b( W) g5 \5 i$ X( h.htaccess 內寫上:
7 {8 e5 |. t8 Z) d: H" z
<Files secret.html>
+ N5 a/ u9 M- B0 _, _# |order allow,deny
) P* s8 i' x' o0 r9 g" odeny from all
; [) d" W5 ^5 m* s! Q7 M; b
</Files>
8 O8 h( l& K+ t+ ^5 S" S# U. h1 k5 Ddeny from all 即所有人皆不能讀取 sercet.html
. ]6 R$ g& k, E2 U# i
5 Z# m" M. ~9 z% O8 X+ h3 Q而 Apache 1.3 以後的版本,更可以用支援 regular expression 的 filesmatch
7 D. I& z& `7 c. A
.htaccess 內寫上:
) O' i0 N6 O5 `8 D8 Q S
<filesmatch "\.jpg">
6 l D2 Y% q3 H$ l. R' B( g1 [ Sorder allow,deny
F- k5 \; [0 d1 \9 f" M# odeny from all
# Q* m4 w( H& Q7 Y2 q</filesmatch>
( Q: w9 U# E( _( I1 A7 Q S- f7 o
即所有人皆不能讀取副檔名為 .jpg 的檔案
- j3 x3 z! H8 U4 ~
0 r+ S0 x* f3 w8 W! U/ j.htaccess 內寫上:
) b+ Q" M, R' d. F# G
<Files secret.html>
" u. I' o: A' `9 n: Yorder allow,deny
) H/ Z5 `6 H! v7 C y
allow form all
" d! t1 J. R* P# ]2 b. Ddeny form 202.202
" ^. V: j% u) H/ M& U+ |</Files>
, S& f- _2 d3 r3 m" F9 l G9 Q: P即 ip 是以 202.202 開頭的不能讀取副檔名為 secret.html 的檔案
- f8 u) _0 ` M9 Q8 F7 O
* Z" }: o1 A. h$ j( P
.htaccess 內寫上:
9 o( J2 u, i7 o. z z U/ E
<Limit GET>
. }; m; W5 D: o8 Torder deny,allow
: }7 U4 n0 T) {) ?2 jdeny from all
; e3 s* K" G4 m l
allow from 202.202 domain.com
3 h1 H+ E4 f. l
</Limit>
. W# ]% _6 ^, q: U7 U \) R. b
即只允許 ip 是以 202.202 為開頭或域名是 domain.com 的讀取此目錄內的任何內容
, c! u# e! e2 _8 \* }8 b
& L# L: T, H |3 u) k7 ]; H.htaccess 內寫上:
$ C2 y' `; ]5 D+ A: M9 R9 C
AuthUserFile "C:/Apache/htdocs/testing/.htpasswd"
! ^8 y. r2 A" o
<files sercet.html>
/ B/ x" X6 Y* C0 n. ^8 arequire user peter
( z3 ^ e9 |+ z( F) l+ C! v</files>
3 @: [) ]- s$ E即只允許 .htpasswd 內列出的 peter 讀取 sercet.html
4 {" W6 B0 b- A' P; ^' C( _' i: w
/ H3 j) Z1 \* Y, B' Q) i, q
5. 重新導向文件
n3 l8 G" }' V9 `
.htaccess 內寫上:
1 E, o7 {7 C& nRedirect /old
http://url/new
" R$ z0 k: D. C F t! Q. Y當用戶要求 /old 時,伺服器便把其重新導向至
http://url/new ,或在 .htaccess 內寫上:
+ j/ V, M+ ?" E+ {( oRedirect permanent /old
http://url/new
5 S# T# }, o, u' f% v# M當用戶要求 /old 時,伺服器便叫瀏覽器把其永遠重新導向至
http://url/new
( H2 s) b- f/ i/ J' `' S7 R
- ?- p/ i- C$ z T* f. \ b6. 防止列出目綠內的檔案
7 J8 E# z; A6 n. b.htaccess 內寫上:
) c! U- h8 X, G0 [9 K5 D
Option -Indexes
+ K& X/ Z* }7 u+ l+ a這便不能列出檔案目錄了
, _0 j& B( G g4 {7 o1 f.htaccess 內寫上:
9 v3 q2 s& c1 c: ~# R8 K# F, K# WIndexIgnore *.zip *.txt
5 h+ o |" F( @: X; t9 E這便不列出檔案目錄內的 .zip 與 .txt 檔了
% ^; |6 v, m# E5 s$ l; ?- j
# F/ e0 t4 p+ x4 H6 u$ z
註: .htaccess 的大部分內容 (如:密碼保護) 可以寫回 httpd.conf 相應的目錄設定內( <Directory> )