[Linux] [轉貼]Apache 網頁認證.htaccess 設定方法
% i$ ]5 p) |9 R9 v: E, G" O* { |" R, o7 f
.htaccess
5 {; S9 x% t( i. r.htaccess 可放在任何一網頁目錄內,當該目錄在 httpd.conf 設定不是 AllowOverride None 便可使用 .htaccess 的功能了,而設定作 AllowOverride All 更可使用 .htaccess 的所有功能了
j6 x, x% z+ n+ a( P5 M.htaccess 可用作對該目錄及其子目錄進行取存控制,而不用修改 httpd.conf 及不用重新啟動伺服器軟件
. P2 I5 h8 s( r: [! q- ?& M--------------------------------------------------------------------------------
" _" j5 o* t; k+ B; y9 Uhttpd.conf 的設定
/ w0 q" W5 B- P& t加上或取消註解 (#) ]定改成 AllowOverride All
1 f% H6 @! Y5 X$ b9 `3 }& i6 @- E然後儲存及重新啟動伺服器軟件
' x' h% E: A" f* Q9 \( G--------------------------------------------------------------------------------
3 [3 M, r T& O.htaccess 的用法
& R5 d; V5 Q4 K7 \/ v9 [$ D4 w
先把 .htaccess 放在要用的目錄內 (Windows 系統內不可以直接把檔案改名作 .htaccess ,需用間接的方法,如在指令模式下 ren filename.txt .htaccess 或用 FTP 軟件修改該檔名)
0 m3 p; I4 Y! P. b用法:
# l, s2 Z* u, Q3 _6 z. j1. 密碼保護
5 K/ w) w" V. }3 x7 t$ S. ~. a& C.htaccess 內寫上:
4 z9 ^) y/ n* p! y8 b, g" x& O& yAuthName "testing"
% h* `2 m4 Y$ }: d/ ^$ M% p- m; ?
AuthType Basic
* f& l* d4 ]3 E2 OAuthUserFile "C:/Apache/htdocs/testing/.htpasswd"
8 b2 C7 A5 d" x0 z% M' lrequire valid-user
- {3 R* j( n/ O6 k3 QAuthName 是當你進入密碼保護時,瀏覽器出現的說明
+ ?# O8 @# [2 Z0 ~9 M( M3 u
AuthUserFile "C:/Apache/htdocs/testing/.htpasswd" 是密碼檔案的位置
/ s+ b5 m# j8 G4 |( l. B
密碼檔案 (.htpasswd) 內寫上:
H$ p- c0 ]$ \user1:password1
" W0 C+ a% W" Cuser2:password2
+ |, K# T+ _* ^' }; n. U
但密碼不應直接寫上,即是如果有一使用者是 test ,密碼是 password ,先用 C:\Apache\bin 內的 htpasswd.exe 把密碼加密,用法如下] require group group1 ,因此只有 group1 的使用者可通過,即 john , peter 及 david
" L: i4 G# G: M, x, w- ^7 r7 g) D- B
2. 自設錯誤報告文件
, | X7 Q$ |" t5 x9 B4 ]- Q.htaccess 內寫上:
* g- C) D2 G; Z. p
ErrorDocument 404 /404.html
9 ^( s! }; @1 J+ d; j
如果找不到網頁時,便輸出 404.html 的內容,其他錯誤時做法亦然,如:
% k' W& L; i& c' Y
ErrorDocument 404 "<html><body>找不到網頁</body></html>
9 z0 }7 q( @, b+ w* T' s: z5 i
ErrorDocument 500 /500.html
) A# ^0 j' h$ N' ]1 H- h. u
1 ~, t) v0 E. ]
3. 自設首頁檔案
) J& j8 Q0 V' t# ]* {.htaccess 內寫上:
- G: `# n" P7 a( v# U
DirectoryIndex a.html
& L2 e; R% u8 J ^1 X ?首頁檔案便是 a.html 了
& Y/ X7 r9 @+ s7 {/ O) C8 T* p
1 }* P$ }" s3 z& P+ _2 g- J
4. 禁止讀取檔案
) r& h X2 F: `" ]0 O. C.htaccess 內寫上:
+ x9 }2 s, u5 b; B/ g0 D. T( n& B) l
<Files secret.html>
( \4 \1 o7 E+ [0 W% L8 uorder allow,deny
, O( c; U; L/ [2 y
deny from all
; g! w+ V4 H$ K3 e1 }0 v; K, C7 H</Files>
* ~" M' V! ~, I0 n1 edeny from all 即所有人皆不能讀取 sercet.html
0 k* @, J$ \+ M/ x. A# s, e
" B$ Y, X/ m4 J! [
而 Apache 1.3 以後的版本,更可以用支援 regular expression 的 filesmatch
, a! c6 a$ A' ^* T.htaccess 內寫上:
5 [/ u1 ~2 E; U1 o. R' P, ?
<filesmatch "\.jpg">
4 j! S9 n- X- k) b1 ^% y% Sorder allow,deny
7 d( t* ]8 _4 e9 ~
deny from all
% [8 \" s3 N2 s" r</filesmatch>
7 y% s }8 G2 s R, G( \( u; O
即所有人皆不能讀取副檔名為 .jpg 的檔案
: ]4 D( v) l" p. Z4 O a
- r$ i8 I- I# O" i
.htaccess 內寫上:
5 e" b6 D, Z0 P# q! I2 `2 y& g' C
<Files secret.html>
8 n% }9 |5 x6 c& T6 h; Morder allow,deny
t5 D$ M" v* N: W x. V6 R4 nallow form all
{0 Z# Y" q1 F; U/ vdeny form 202.202
0 B+ O0 c6 p! M! R5 ]</Files>
3 }0 l& v* u1 J8 w5 E2 x1 y
即 ip 是以 202.202 開頭的不能讀取副檔名為 secret.html 的檔案
8 h& h) J B+ l- r: i4 {( ]! a4 t
9 c" S* J. y% e. l, v.htaccess 內寫上:
( U0 J# E0 u, t/ q" T6 W9 C0 p F
<Limit GET>
8 D5 K3 ~$ s* `. A* y* k9 }5 forder deny,allow
L" O5 P1 X) W/ _& Y1 H* S7 [, F+ y
deny from all
) A( w$ y2 ]$ O ^8 D/ vallow from 202.202 domain.com
1 a0 z( z( l& X# E6 b</Limit>
. M6 Z5 M8 B; a8 g即只允許 ip 是以 202.202 為開頭或域名是 domain.com 的讀取此目錄內的任何內容
$ n n7 @$ l! [( D
7 q' U6 u) @) \8 c8 |* [
.htaccess 內寫上:
/ u# y) X+ `& T/ t9 u% a# \AuthUserFile "C:/Apache/htdocs/testing/.htpasswd"
( v' U, X1 L: [; m" h; a<files sercet.html>
7 Q' g0 }% w8 u: S) ?% _ L. _
require user peter
% H8 o8 u0 A" H: d2 m
</files>
+ V3 Q6 e* i/ g4 M& w6 G# E
即只允許 .htpasswd 內列出的 peter 讀取 sercet.html
- t2 B, M, b( [- z, \' c
! ^) {! ?- `. H( b2 X5. 重新導向文件
2 R# o7 t- J+ G9 O0 W) \, K. Y, x
.htaccess 內寫上:
6 b: \0 x7 @2 ?$ c) R+ bRedirect /old
http://url/new
/ ^% w. ~- [5 N& j/ D Y5 w" y當用戶要求 /old 時,伺服器便把其重新導向至
http://url/new ,或在 .htaccess 內寫上:
$ Z7 t+ r& e! r9 v
Redirect permanent /old
http://url/new
6 u5 L' e+ w }- k2 S. b1 ?6 _3 \當用戶要求 /old 時,伺服器便叫瀏覽器把其永遠重新導向至
http://url/new
5 S/ G) @9 ]5 y5 e: r" K0 Q2 ?! v& k
) H0 z& f( } N, m% L) J! P6. 防止列出目綠內的檔案
. [7 S1 m1 H$ w3 A3 o.htaccess 內寫上:
! c& Y. y1 u, \( j( X# R7 ~
Option -Indexes
& C, d( S% m5 _* O$ h
這便不能列出檔案目錄了
6 P8 U. F ^% Q% I, S$ o4 F" ?! v9 @. K
.htaccess 內寫上:
! j, w; T7 t0 f2 P# I
IndexIgnore *.zip *.txt
: l* \# w& r. T' b9 b1 n這便不列出檔案目錄內的 .zip 與 .txt 檔了
, h0 g2 m8 h- b
' q, n1 t" s& Y D* e/ W3 g7 K註: .htaccess 的大部分內容 (如:密碼保護) 可以寫回 httpd.conf 相應的目錄設定內( <Directory> )