|
环境:系统 Windows+Apache 2.2$ \/ G6 R6 C- S
' P1 {$ c9 {7 Q( A- d( _- y* ]加载Rewrite模块:' o: c8 E( }% ~
在conf目录下httpd.conf中找到 LoadModule rewrite_module modules/mod_rewrite.so 这句,去掉前边的注释符号“#”,或添加这句。
) n; T! C" B$ a! I; |
& v/ s+ u1 H0 r' \允许在任何目录中使用“.htaccess”文件,将“AllowOverride”改成“All”(默认为“None”):8 I3 b- K: a' m" w' o4 p* G% P
; z" o5 D! O( @# G) I
# AllowOverride controls what directives may be placed in .htaccess files.! p' b$ T0 e/ v- e ?7 x
# It can be “All”, “None”, or any combination of the keywords:
: B- T% p) a; v% b0 T; B- s2 n# Options FileInfo AuthConfig Limit- [' C5 V" y1 Q4 r) v! w* M
#
3 r, M; s2 \" lAllowOverride All% q6 _# K0 X- U2 j5 ~( N: y
. K N7 T( m9 ]( s3 O6 \在Windows系统下不能直接创建“.htaccess”文件,可以在命令行下使用“echo a> .htaccess”建立,然后使用记事本编辑。或者,系统设置显示扩展文件名,打开记事本另存为.htaccess文件,文件类型,所有文件。
7 [9 Q2 T" t9 g5 Q( h" v: @- D9 p* ~ [5 t! @+ }# I9 }
Apache Rewrite模块的简单应用:. E$ @# q3 r2 _; A; g$ b1 ?8 r; x
Rewrite的所有判断规则均基于Perl风格的正则表达式,通过以下基础示例能写出符合自己跳转需求的代码。
! K* u! u* c% {: P O% R
/ W* V5 p& M4 d% M5 j" p1、请求跳转
' I* m) F- Q" H; |目的是如果请求为.jsp文件,则跳转至其它域名访问。! H5 L3 a. s7 h# R: Z# [0 N
例如:访问www.xp6.org/a.php跳转至b.xp6.org/b.php网页,访问www.xp6.org/news/index.php跳转至b.xp6.org/news/index.php网页1 c3 a2 n0 ?6 r2 O* w2 I- Z
注意:不是使用HTML技术中的meta或者javascript方式,因为www.xp6.org/a.php这个文件并不存在,用的是Apache2.2服务器中的Rewrite模块。
, s! h& k! u A; B& D0 ?7 k) K修改 .htaccess或apche的配置文件httpd.conf文件,添加以下内容1 J& `% ?9 L6 d+ i$ ~
- RewriteEngine on& u3 B; w5 W! m. [+ E) Q [
- #开启Rewrite模块
/ k2 c* y5 [3 w+ T - RewriteRule (.*)\.php$ http://www.xp6.org/$1\.jsp [R=301,L,NC]
复制代码 #截获所有.jsp请求,跳转到http://b.xp6.org/加上原来的请求再加上.php。R=301为301跳转,L为rewrite规则到此终止,NC为不区分大小写: @. {- {; R% D- q* p3 ]' `; @
2、域名跳转
/ a0 ^9 e. u1 T+ U( d' o7 K如果请求为old.xp6.org下的所有URL,跳转至b." F6 z. o9 W, D! Q* B( f$ ]
- RewriteEngine on" H$ o5 \7 G- V+ E! b9 q1 r7 ~- e1 G# d
- #开启Rewrite模块* j1 [: U/ R+ W
- RewriteCond %{REMOTE_HOST} ^old.xp6.org$ [NC]9 A9 k* c6 o u0 l" H: F
- #针对host为old.xp6.org的主机做处理,^为开始字符,$为结尾字符
6 h% `/ O; m' w2 ]% d - RewriteRule (.*) http://b.xp6.org/$1 [R=301,L,NC]
复制代码 3、防盗链0 {: _/ v7 K% f1 a" Z5 n) p3 A# W8 c
如果本网站的图片不想让其它网站调用,可以在 .htaccess或者apche的配置文件httpd.conf文件中添加以下内容5 [$ ]- c# t4 ~
- RewriteEngine on
% w( T/ _9 Y H" [5 t0 m1 N, ~ - #开启Rewrite模块6 s7 e* X+ ?: ~" k7 z
- RewriteCond %{HTTP_REFERER} !^$ B. E( b3 m/ M
- #如果不是直接输入图片地址
/ m# F( p/ y. ^6 `: X8 Z# E - RewriteCond %{HTTP_REFERER} !img.xp6.org$ [NC]
5 z/ Z! E1 o" u J. w2 i' b - #且如果不是img.xp6.org所有子域名调用的
8 q' L0 D- ^3 K$ x# B+ p - RewriteCond %{HTTP_REFERER} !img.xp6.org/(.*)$ [NC]: v) x, c7 I) P0 O6 g7 ?" l3 d7 L1 |
- RewriteCond %{HTTP_REFERER} !qq.com [NC]$ J V. o1 G# n6 B8 W' @; U
- RewriteCond %{HTTP_REFERER} !google.com [NC]
. \/ C( c! R$ G: ~' Y' K8 P2 Y; a - RewriteCond %{HTTP_REFERER} !google.cn [NC]
& W# Y8 ~ @' p2 @' }* v6 b9 f6 H) y - RewriteCond %{HTTP_REFERER} !baidu.com [NC]3 r# G* ?4 x/ l7 y
- RewriteCond %{HTTP_REFERER} !feedsky.com [NC]
; n* {5 k& h7 |. m( A: N& ^& n( 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文件
% o; p7 }9 u/ `9 B" q在Apache2\conf\httpd.conf 最后一行添加2 ?5 a6 `& u8 f( u3 l9 s
- RewriteEngine On
. a$ M2 |. _& P - RewriteRule ^(.*)-htm-(.*)$ $1.php?$2
复制代码 重启Apache,登陆后台开启全伪3 S6 ?* b: H' ]) X8 v* u
3 I4 D6 |- v! C9 G: g& O* V4 _
Linux+Apache环境配置类似。
" g7 }0 V$ r0 F7 S2 r: Q
+ _2 ~" |7 O; e7 @# A |
|