|
环境:系统 Windows+Apache 2.2$ x5 ?! w+ N; ?
6 L- a7 m. e2 Y- O3 A4 v2 V* G! G加载Rewrite模块:( I) A: Y2 @1 w# _& O. x9 U: D
在conf目录下httpd.conf中找到 LoadModule rewrite_module modules/mod_rewrite.so 这句,去掉前边的注释符号“#”,或添加这句。! h! `+ p N* ?
7 `4 ^4 T, b9 J5 J
允许在任何目录中使用“.htaccess”文件,将“AllowOverride”改成“All”(默认为“None”):
7 `; p" [; q+ L, N
- j) ]% Z. j5 I% v" s# q# AllowOverride controls what directives may be placed in .htaccess files.
1 j& y8 F: a$ ~' Y; L9 M: Y# It can be “All”, “None”, or any combination of the keywords:
+ V1 E3 E' A+ N' e6 s4 l# r2 E# Options FileInfo AuthConfig Limit8 X5 O+ W& @8 K) V
#
) {9 a0 `2 V' MAllowOverride All
! k3 B7 Q" q, q
7 }" \$ J6 [1 S$ `+ Q B- r在Windows系统下不能直接创建“.htaccess”文件,可以在命令行下使用“echo a> .htaccess”建立,然后使用记事本编辑。或者,系统设置显示扩展文件名,打开记事本另存为.htaccess文件,文件类型,所有文件。$ `) e' w8 i. @9 ]3 ]) K
% s) P4 Q. Y0 x" x7 cApache Rewrite模块的简单应用:
0 y# M: H" p1 a# Y& J) ZRewrite的所有判断规则均基于Perl风格的正则表达式,通过以下基础示例能写出符合自己跳转需求的代码。
7 p& i K3 E, V& T' i( @% U" ]9 i% ^) {: |3 h, P
1、请求跳转6 K5 L% a) D9 D @9 E
目的是如果请求为.jsp文件,则跳转至其它域名访问。: z9 f4 [- Q* h: ]$ H5 M1 n# H5 Q
例如:访问www.xp6.org/a.php跳转至b.xp6.org/b.php网页,访问www.xp6.org/news/index.php跳转至b.xp6.org/news/index.php网页
E: c3 F& N! N( k% f; [注意:不是使用HTML技术中的meta或者javascript方式,因为www.xp6.org/a.php这个文件并不存在,用的是Apache2.2服务器中的Rewrite模块。3 p+ g' v7 Y( r! F6 o- K: @( j5 y
修改 .htaccess或apche的配置文件httpd.conf文件,添加以下内容
5 F4 M, \% x0 Z* B% s7 J z' M2 r- RewriteEngine on
3 [8 a W" r& U ^& q/ M# T* u% K - #开启Rewrite模块
$ ^8 j, E1 Q# [9 `; J - RewriteRule (.*)\.php$ http://www.xp6.org/$1\.jsp [R=301,L,NC]
复制代码 #截获所有.jsp请求,跳转到http://b.xp6.org/加上原来的请求再加上.php。R=301为301跳转,L为rewrite规则到此终止,NC为不区分大小写
, {% _$ ^, q( V2 b. u( G: K5 K2、域名跳转1 b' y$ c) p+ \
如果请求为old.xp6.org下的所有URL,跳转至b.4 ~. Z5 V3 x$ M0 p! B9 ~( e- Y" N
- RewriteEngine on
4 V' c. h0 h8 m: C+ ]# I7 l" `& V - #开启Rewrite模块
7 A+ z% e- i; W e5 }. P# B - RewriteCond %{REMOTE_HOST} ^old.xp6.org$ [NC]- M2 ]8 `! J/ E8 |1 r- K
- #针对host为old.xp6.org的主机做处理,^为开始字符,$为结尾字符; J# B7 E: \: o i6 H' a/ h
- RewriteRule (.*) http://b.xp6.org/$1 [R=301,L,NC]
复制代码 3、防盗链9 H2 }: q7 C! N; U3 w" r* q
如果本网站的图片不想让其它网站调用,可以在 .htaccess或者apche的配置文件httpd.conf文件中添加以下内容
; D. M4 H) m( P, M+ n U" I5 s- RewriteEngine on
) B/ D* K$ J8 G8 C - #开启Rewrite模块0 f3 [. K X& u0 W- I! C
- RewriteCond %{HTTP_REFERER} !^$3 v0 l: }2 s# E( x# \
- #如果不是直接输入图片地址
+ [# t5 t: k' r% |8 o - RewriteCond %{HTTP_REFERER} !img.xp6.org$ [NC]1 g. V: G2 p7 D3 M9 c; n
- #且如果不是img.xp6.org所有子域名调用的
6 ]6 c7 I/ D8 e* V9 k' P, X - RewriteCond %{HTTP_REFERER} !img.xp6.org/(.*)$ [NC]1 R6 c- P1 O) W- ^* w
- RewriteCond %{HTTP_REFERER} !qq.com [NC]
G- n. ~; T+ g - RewriteCond %{HTTP_REFERER} !google.com [NC]4 N1 U x, n$ C8 G5 l! C) j
- RewriteCond %{HTTP_REFERER} !google.cn [NC]! V1 [( j% D) W$ A9 P/ ~
- RewriteCond %{HTTP_REFERER} !baidu.com [NC]6 A3 n8 N5 w! o
- RewriteCond %{HTTP_REFERER} !feedsky.com [NC]
; z3 a# j5 y# R n - 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文件2 r- |2 v/ t" p( b4 j; _' H
在Apache2\conf\httpd.conf 最后一行添加
& i5 w. U0 P" t5 N! j: r# h- RewriteEngine On4 q- A# X+ c! G+ C- G
- RewriteRule ^(.*)-htm-(.*)$ $1.php?$2
复制代码 重启Apache,登陆后台开启全伪
; Y, L. A; D4 I- B x- q i& v6 x' [5 c! e/ Q4 i( K
Linux+Apache环境配置类似。
" |8 M1 G; W( _8 s0 j, G c1 s
" `+ Q( x1 c" d5 p |
|