[Linux] [轉貼]Apache 網頁認證.htaccess 設定方法
1 W) d2 G+ f/ }4 ?! [6 p
1 E# h5 {- @6 |6 w.htaccess
- Z+ ]/ U Z4 h) ~4 V* ]
.htaccess 可放在任何一網頁目錄內,當該目錄在 httpd.conf 設定不是 AllowOverride None 便可使用 .htaccess 的功能了,而設定作 AllowOverride All 更可使用 .htaccess 的所有功能了
) d9 t* s: e0 i) L9 ~+ K9 n- Q
.htaccess 可用作對該目錄及其子目錄進行取存控制,而不用修改 httpd.conf 及不用重新啟動伺服器軟件
5 E# i) T. g) Q6 e--------------------------------------------------------------------------------
: A3 g* `$ Y8 x6 h, Ehttpd.conf 的設定
2 K) {9 C% P# p0 a/ Y
加上或取消註解 (#) ]定改成 AllowOverride All
4 D! b8 { f( s8 h
然後儲存及重新啟動伺服器軟件
. v+ M+ i+ }. `4 c! k
--------------------------------------------------------------------------------
; g( b/ h$ _' |, q4 j( d.htaccess 的用法
- Z( _% e9 J$ t7 }先把 .htaccess 放在要用的目錄內 (Windows 系統內不可以直接把檔案改名作 .htaccess ,需用間接的方法,如在指令模式下 ren filename.txt .htaccess 或用 FTP 軟件修改該檔名)
/ [' o$ `2 u4 y4 Z$ R
用法:
3 i C% s: [/ b! X5 F1. 密碼保護
) g$ S1 ~% S1 ~1 {: h1 ~ h.htaccess 內寫上:
& \% u" I* {3 ^1 `" `AuthName "testing"
0 K8 W+ p4 T# s4 R
AuthType Basic
+ [. V! ]! s, W) f/ ^6 D$ wAuthUserFile "C:/Apache/htdocs/testing/.htpasswd"
; [8 E+ f: x8 O4 ~require valid-user
" A- J% B' H# D1 [AuthName 是當你進入密碼保護時,瀏覽器出現的說明
0 T3 J7 N% ~6 g) k4 {4 GAuthUserFile "C:/Apache/htdocs/testing/.htpasswd" 是密碼檔案的位置
, g4 I) ], @- H! d, r
密碼檔案 (.htpasswd) 內寫上:
( t0 h! G; F+ \# Suser1:password1
- V3 R3 K' x3 L
user2:password2
( S+ u: E' s+ Z7 }但密碼不應直接寫上,即是如果有一使用者是 test ,密碼是 password ,先用 C:\Apache\bin 內的 htpasswd.exe 把密碼加密,用法如下] require group group1 ,因此只有 group1 的使用者可通過,即 john , peter 及 david
( B8 h. `* |4 H) `
9 N/ ~5 \% f( V$ _! N: S% z a( A" s2 f
2. 自設錯誤報告文件
# |) M+ ~/ O+ M) j9 g2 r7 J- U.htaccess 內寫上:
3 d7 q3 b+ T Z X9 [: dErrorDocument 404 /404.html
4 X1 Q' Z5 Q: {1 ?
如果找不到網頁時,便輸出 404.html 的內容,其他錯誤時做法亦然,如:
) q( p/ j- k8 R+ ?# M2 Y
ErrorDocument 404 "<html><body>找不到網頁</body></html>
! d/ ^) g; Y- j1 h; nErrorDocument 500 /500.html
6 ^7 o e) R' I" S
- h0 y1 |; R H |! F
3. 自設首頁檔案
& {0 y0 T7 u$ E _5 L. q8 ]/ {.htaccess 內寫上:
) d/ h- ?/ Q3 B- S" R- E! y! i5 H. @DirectoryIndex a.html
+ n% a" [+ [3 [- n) g5 V
首頁檔案便是 a.html 了
' ]. U+ B* A, S- l' y: Z% e- Y8 w |& E. K! ?8 Z
4. 禁止讀取檔案
7 g6 k6 n7 L" r* P.htaccess 內寫上:
. F# T& Y2 M+ o5 j3 @) R<Files secret.html>
( ]; t8 v1 d# r8 L/ D+ Q8 O
order allow,deny
' p4 d+ K0 V* ^. \2 _% L! B
deny from all
0 L5 m8 w) I6 n" L4 n- J' C
</Files>
5 k) v* g7 m8 W: z5 i4 n* j
deny from all 即所有人皆不能讀取 sercet.html
: k U P5 k7 N, g' k: s0 }
" `; b( K7 l" G; n6 P9 E! j而 Apache 1.3 以後的版本,更可以用支援 regular expression 的 filesmatch
A) F! i. g c: V# j
.htaccess 內寫上:
1 I* e& ]% Y# K P* S<filesmatch "\.jpg">
" ~7 Z, M0 u3 d& S6 E oorder allow,deny
! a/ [' U* h" j" `( {deny from all
_# q. ]2 C2 W$ }" y# M1 Z; ~3 b) q% P</filesmatch>
! A; }5 k! s* C5 Q5 r: G. V即所有人皆不能讀取副檔名為 .jpg 的檔案
: G" ~9 ^0 T! m( z( `9 h- y! K) h
.htaccess 內寫上:
2 W w# c2 M$ O<Files secret.html>
$ O3 A+ u6 O2 C0 f) e: Forder allow,deny
. p- F9 F' d0 pallow form all
1 z" A4 l% X$ A3 ^ |
deny form 202.202
# ?3 a& B& p' ~; D1 s
</Files>
2 o$ P1 F2 y/ x! m9 t即 ip 是以 202.202 開頭的不能讀取副檔名為 secret.html 的檔案
$ G$ o) u2 S% y% X
6 t8 }4 M- U4 d6 j.htaccess 內寫上:
& s- Y7 L, V/ F
<Limit GET>
$ P8 A% t# K- b2 P [4 k% morder deny,allow
2 ]+ e0 j1 z& B. }4 @% E. pdeny from all
% B4 K3 ?+ l( j% \8 V- ~
allow from 202.202 domain.com
E6 {' \3 I% N; T
</Limit>
1 m0 B5 \6 q- D5 T即只允許 ip 是以 202.202 為開頭或域名是 domain.com 的讀取此目錄內的任何內容
D7 F1 a& d& K- m( K4 W2 m( B# y2 c
, A; F! p& {" S4 _ ?
.htaccess 內寫上:
* A: X7 j% P6 ?( |# Y7 E4 E% ZAuthUserFile "C:/Apache/htdocs/testing/.htpasswd"
( k' m6 _! n$ n, T' e. u( R<files sercet.html>
+ f! D" A$ J7 ~8 v/ q+ c
require user peter
' \6 ]9 l; ?! D) k3 J
</files>
3 J5 h( s) R5 M即只允許 .htpasswd 內列出的 peter 讀取 sercet.html
V$ o; r3 ^$ `$ \* U4 \1 x- {3 A# Z% s. u
5. 重新導向文件
# Q7 X+ | D4 J. V
.htaccess 內寫上:
. z7 P# v8 D! Y( b4 v- [Redirect /old
http://url/new/ U, [6 J( l. A4 i
當用戶要求 /old 時,伺服器便把其重新導向至
http://url/new ,或在 .htaccess 內寫上:
: m) U j& P _( n
Redirect permanent /old
http://url/new
$ ^( O d8 V o4 D當用戶要求 /old 時,伺服器便叫瀏覽器把其永遠重新導向至
http://url/new2 x; s& p5 q8 B
# Y9 K! }# z# C1 B- O
6. 防止列出目綠內的檔案
; n' \% r, s7 h0 ^.htaccess 內寫上:
# f9 x+ \0 A% [5 X+ T. T
Option -Indexes
% l3 O6 `, A0 z/ W4 o2 J9 E這便不能列出檔案目錄了
/ [# I; N Z; @5 S/ _
.htaccess 內寫上:
9 ~, @5 |$ b' s& G# K aIndexIgnore *.zip *.txt
0 z- v' l( t. i2 Y/ s: @6 q4 w這便不列出檔案目錄內的 .zip 與 .txt 檔了
' s( s- C% \$ x8 }, x0 f5 G0 K/ D K* m* U% {- F$ {1 T5 r
註: .htaccess 的大部分內容 (如:密碼保護) 可以寫回 httpd.conf 相應的目錄設定內( <Directory> )