|
环境:系统 Windows+Apache 2.2
0 l% _0 G, T. R& l
, P/ N( I# N) x5 Z H$ ^# ?加载Rewrite模块:
. G! p, M5 D5 X5 Y. ^在conf目录下httpd.conf中找到 LoadModule rewrite_module modules/mod_rewrite.so 这句,去掉前边的注释符号“#”,或添加这句。; I" v4 b( Q+ v3 x
% _9 a& {3 q) R7 {: ?) u9 F# a2 b允许在任何目录中使用“.htaccess”文件,将“AllowOverride”改成“All”(默认为“None”):# K: ^' |7 O: P$ x
! {( M$ X0 K' l# AllowOverride controls what directives may be placed in .htaccess files.
& |% C1 p7 W6 L" z3 v# It can be “All”, “None”, or any combination of the keywords:
. |0 U0 x) O$ u# Options FileInfo AuthConfig Limit5 v: Q1 H, C, F. M+ t
#0 C% I) @& V; k0 H: H
AllowOverride All
5 [4 V, B; {6 ^, B! u5 h |- J3 {7 }" z
在Windows系统下不能直接创建“.htaccess”文件,可以在命令行下使用“echo a> .htaccess”建立,然后使用记事本编辑。或者,系统设置显示扩展文件名,打开记事本另存为.htaccess文件,文件类型,所有文件。; Y0 u" ~9 `) o2 R* x/ ^, V
: b( E* F4 z9 kApache Rewrite模块的简单应用:) V4 F) c5 D0 c! U3 R
Rewrite的所有判断规则均基于Perl风格的正则表达式,通过以下基础示例能写出符合自己跳转需求的代码。
& e$ T r. B; Z: Y+ G% r# p% `! o9 Y4 [5 [
1、请求跳转
) F4 P- e2 R5 ^; H! W目的是如果请求为.jsp文件,则跳转至其它域名访问。
; a2 g8 z2 ]' ?9 b( ~# V8 `( S例如:访问www.xp6.org/a.php跳转至b.xp6.org/b.php网页,访问www.xp6.org/news/index.php跳转至b.xp6.org/news/index.php网页
# s9 s* [: a' }1 {2 A+ E5 l注意:不是使用HTML技术中的meta或者javascript方式,因为www.xp6.org/a.php这个文件并不存在,用的是Apache2.2服务器中的Rewrite模块。5 ^* o% H: J; I2 o3 X
修改 .htaccess或apche的配置文件httpd.conf文件,添加以下内容
+ d9 l1 Z; ` K: @- RewriteEngine on
# b0 v( u1 M9 p; T+ j9 N - #开启Rewrite模块- @$ V: q& z- a5 l
- RewriteRule (.*)\.php$ http://www.xp6.org/$1\.jsp [R=301,L,NC]
复制代码 #截获所有.jsp请求,跳转到http://b.xp6.org/加上原来的请求再加上.php。R=301为301跳转,L为rewrite规则到此终止,NC为不区分大小写
8 s0 Y6 N& a" ?- b; v( l2、域名跳转
% s' W2 _8 R1 L9 s如果请求为old.xp6.org下的所有URL,跳转至b.
$ `* K/ d7 \+ N' _& e( G2 [& |- RewriteEngine on
# ?% `) S. m$ o2 H# F - #开启Rewrite模块
$ V m! T& h# \2 u9 L j9 f( N, ` - RewriteCond %{REMOTE_HOST} ^old.xp6.org$ [NC]
' v7 m6 |& b/ m7 q) z - #针对host为old.xp6.org的主机做处理,^为开始字符,$为结尾字符
% \8 w5 n7 Z" x8 { k - RewriteRule (.*) http://b.xp6.org/$1 [R=301,L,NC]
复制代码 3、防盗链
6 o+ F! ?: a/ k4 w: \& |如果本网站的图片不想让其它网站调用,可以在 .htaccess或者apche的配置文件httpd.conf文件中添加以下内容' K2 K8 U# K' X4 ^! k
- RewriteEngine on
! F. b5 J0 p( t7 P: f$ o, H8 m" T( l* t - #开启Rewrite模块
- J! d* r7 e7 |, {8 A" a0 e/ I - RewriteCond %{HTTP_REFERER} !^$0 ~# P; b% S, O# L% d/ L* n) a& b
- #如果不是直接输入图片地址
) i: n3 b1 f1 M/ M; ?9 u - RewriteCond %{HTTP_REFERER} !img.xp6.org$ [NC]
9 q5 d3 f* i! t, r+ j - #且如果不是img.xp6.org所有子域名调用的6 I; S5 z* J* |# |8 @! H
- RewriteCond %{HTTP_REFERER} !img.xp6.org/(.*)$ [NC]! W& h' q' d, L4 `
- RewriteCond %{HTTP_REFERER} !qq.com [NC]- u8 P# B7 v. x# J; l
- RewriteCond %{HTTP_REFERER} !google.com [NC]
) ^! _, q( W0 s( U# d - RewriteCond %{HTTP_REFERER} !google.cn [NC]
u1 y/ D" I5 O; { - RewriteCond %{HTTP_REFERER} !baidu.com [NC]
+ f+ J; o! L6 V, L - RewriteCond %{HTTP_REFERER} !feedsky.com [NC]
, @7 R9 Z2 r: a1 E } - RewriteRule (.*)\.(jpg|jpeg|jpe|gif|bmp|png|wma|mp3|wav|avi|mp4|flv|swf)$ http://xp6.org/err.jpg [R=301,L,NC]
复制代码 4、不需要定义.htaccess文件5 Q( d9 J+ A% c# a# V( o1 p
在Apache2\conf\httpd.conf 最后一行添加/ x' z9 h! @+ y% C; x; o
- RewriteEngine On
* u* r! N- S& f - RewriteRule ^(.*)-htm-(.*)$ $1.php?$2
复制代码 重启Apache,登陆后台开启全伪0 ~, d+ _9 @' `% T
. r8 B9 F2 g# q. r1 _
Linux+Apache环境配置类似。
1 v8 D: \0 t* s5 X2 \5 w$ n# j, n
! p& a! a% g1 T. U% \& G7 n8 S |
|