|
环境:系统 Windows+Apache 2.2
( ]0 U6 z) m: J( y6 j
5 O5 f: Z- ~( ~$ t: P8 _加载Rewrite模块:
- z0 T6 e/ W; W$ w3 L2 K ?在conf目录下httpd.conf中找到 LoadModule rewrite_module modules/mod_rewrite.so 这句,去掉前边的注释符号“#”,或添加这句。
9 L: N+ S! V7 p# t; j5 A8 v$ o G& s1 L7 h
允许在任何目录中使用“.htaccess”文件,将“AllowOverride”改成“All”(默认为“None”):- J4 c9 ?1 N3 u; t& T' ~
" O2 ^7 ^8 f, i- e
# AllowOverride controls what directives may be placed in .htaccess files.
1 ?3 ?9 Q1 X3 R9 J# It can be “All”, “None”, or any combination of the keywords:
# ]$ G7 {8 n* g# Options FileInfo AuthConfig Limit
: u$ T+ C5 z( O' D5 J* c9 R#2 @& c" Y$ ~# Y5 a9 u# n9 t) D
AllowOverride All
1 P7 x5 P# q5 |, x6 c
. b. j" q/ C- g) E0 V5 [在Windows系统下不能直接创建“.htaccess”文件,可以在命令行下使用“echo a> .htaccess”建立,然后使用记事本编辑。或者,系统设置显示扩展文件名,打开记事本另存为.htaccess文件,文件类型,所有文件。
5 l* m/ s( Z( z* X# X3 z; M) r: l6 c q* b- D' N9 @$ `
Apache Rewrite模块的简单应用:) B- X( o7 \; j, v2 K5 d
Rewrite的所有判断规则均基于Perl风格的正则表达式,通过以下基础示例能写出符合自己跳转需求的代码。! b# Y+ T) t. r0 I* C/ F
6 x: d. D' Q& E9 X, [' ^1、请求跳转
, C) O* D+ m- R目的是如果请求为.jsp文件,则跳转至其它域名访问。
/ G2 M7 C R& m0 p9 n6 @; h例如:访问www.xp6.org/a.php跳转至b.xp6.org/b.php网页,访问www.xp6.org/news/index.php跳转至b.xp6.org/news/index.php网页7 q; j5 w% u( q1 H" p* Z' O
注意:不是使用HTML技术中的meta或者javascript方式,因为www.xp6.org/a.php这个文件并不存在,用的是Apache2.2服务器中的Rewrite模块。
* h, ?9 T/ s* A7 A4 u修改 .htaccess或apche的配置文件httpd.conf文件,添加以下内容) _3 g; R0 D" [/ M2 S
- RewriteEngine on
+ F c+ M; N7 ] - #开启Rewrite模块- P }- r! k) {) h) } R
- RewriteRule (.*)\.php$ http://www.xp6.org/$1\.jsp [R=301,L,NC]
复制代码 #截获所有.jsp请求,跳转到http://b.xp6.org/加上原来的请求再加上.php。R=301为301跳转,L为rewrite规则到此终止,NC为不区分大小写
" ], s8 {5 R$ l2 \6 A2 n2、域名跳转: w8 z' L- T$ [3 i, X8 E) t
如果请求为old.xp6.org下的所有URL,跳转至b.6 t$ N- ?2 @# y9 G
- RewriteEngine on
' o1 O9 M! b, P. O. q% r# r - #开启Rewrite模块% k: }" @1 X) {
- RewriteCond %{REMOTE_HOST} ^old.xp6.org$ [NC]' x5 c- f5 \/ A$ D& x0 h
- #针对host为old.xp6.org的主机做处理,^为开始字符,$为结尾字符
" j3 s0 q4 m" g3 n3 H4 u - RewriteRule (.*) http://b.xp6.org/$1 [R=301,L,NC]
复制代码 3、防盗链8 G4 ], W3 S0 W6 H7 |& E! t
如果本网站的图片不想让其它网站调用,可以在 .htaccess或者apche的配置文件httpd.conf文件中添加以下内容
* h7 {- b1 k7 J6 F9 M, K2 I- RewriteEngine on- C3 X- q0 s& u
- #开启Rewrite模块
4 \- Q4 q( j" ? - RewriteCond %{HTTP_REFERER} !^$
9 V- C$ o) S4 _- | - #如果不是直接输入图片地址
# ~& q8 P: v9 p+ z - RewriteCond %{HTTP_REFERER} !img.xp6.org$ [NC]
* d) E2 H. J a7 S1 [8 n2 M' L - #且如果不是img.xp6.org所有子域名调用的* L5 K3 F/ S ^ J4 p: b5 l. @$ w
- RewriteCond %{HTTP_REFERER} !img.xp6.org/(.*)$ [NC]
) z7 F4 t$ f6 R) J+ L - RewriteCond %{HTTP_REFERER} !qq.com [NC]! _. D" j/ {+ a
- RewriteCond %{HTTP_REFERER} !google.com [NC]2 ~% a3 H6 D" z' q, L' }( P
- RewriteCond %{HTTP_REFERER} !google.cn [NC] K/ H1 H& a! P# `- K
- RewriteCond %{HTTP_REFERER} !baidu.com [NC]
% J/ j5 i/ }/ R' n9 c - RewriteCond %{HTTP_REFERER} !feedsky.com [NC]) l: m3 V9 f h6 X* h
- 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文件8 h0 t0 v1 R) r) W: U' L; h0 M" P
在Apache2\conf\httpd.conf 最后一行添加 F2 A) {( t+ p" ?+ t3 M
- RewriteEngine On
5 j, Q; K) K8 x) ^ n. f - RewriteRule ^(.*)-htm-(.*)$ $1.php?$2
复制代码 重启Apache,登陆后台开启全伪
/ ~- A4 d5 v5 C+ |, R/ S: x I0 S' }
Linux+Apache环境配置类似。5 y& O/ B$ v' S6 W! O: v
' A3 s. @( R4 y# ~$ d
|
|