|
|
环境:系统 Windows+Apache 2.2
( b0 j1 d f8 G+ y1 m% h5 [
2 C4 m& _* G. l加载Rewrite模块:
( [3 R. ~+ M) i, I& h6 p* _4 S在conf目录下httpd.conf中找到 LoadModule rewrite_module modules/mod_rewrite.so 这句,去掉前边的注释符号“#”,或添加这句。. g( [/ h$ b, }' y
4 b8 L9 E1 B! U I! s% G: B( P! i允许在任何目录中使用“.htaccess”文件,将“AllowOverride”改成“All”(默认为“None”):
; v( k, a% l6 k& C) h, J/ Y8 Q
# AllowOverride controls what directives may be placed in .htaccess files.' c+ }; _4 z* k! u8 M0 x" m
# It can be “All”, “None”, or any combination of the keywords:- F( v: _0 L* b
# Options FileInfo AuthConfig Limit ~& D5 `- K. {* u* h4 [6 O
#2 f: U/ s6 j: `6 W; Z1 k
AllowOverride All8 w/ I/ N1 n% m3 f( |
# R1 g9 ~8 H# `5 g4 [- b y在Windows系统下不能直接创建“.htaccess”文件,可以在命令行下使用“echo a> .htaccess”建立,然后使用记事本编辑。或者,系统设置显示扩展文件名,打开记事本另存为.htaccess文件,文件类型,所有文件。' c2 Q% U' x' N1 j) b: Q
% i7 k/ I8 I9 P8 v' e. W$ j% ]Apache Rewrite模块的简单应用:
8 D8 ]5 U# k s5 H9 ?8 ]& ^Rewrite的所有判断规则均基于Perl风格的正则表达式,通过以下基础示例能写出符合自己跳转需求的代码。+ S _. I; a/ ~9 ]% f
: N, L6 l( F t! w1、请求跳转
) O6 l" h- _ v# f: ~, d# A/ w" H目的是如果请求为.jsp文件,则跳转至其它域名访问。
, d. a% S. I# n$ P& C3 l0 I' I例如:访问www.xp6.org/a.php跳转至b.xp6.org/b.php网页,访问www.xp6.org/news/index.php跳转至b.xp6.org/news/index.php网页
+ ^# M9 s E8 D; C" G( n注意:不是使用HTML技术中的meta或者javascript方式,因为www.xp6.org/a.php这个文件并不存在,用的是Apache2.2服务器中的Rewrite模块。. t; r4 U6 D7 |. `% _
修改 .htaccess或apche的配置文件httpd.conf文件,添加以下内容7 {( P+ h: ?0 \6 {9 m" d
- RewriteEngine on
7 P X* ?. [ \! ^ - #开启Rewrite模块
, {, P$ ^3 Z6 F! J! D - RewriteRule (.*)\.php$ http://www.xp6.org/$1\.jsp [R=301,L,NC]
复制代码 #截获所有.jsp请求,跳转到http://b.xp6.org/加上原来的请求再加上.php。R=301为301跳转,L为rewrite规则到此终止,NC为不区分大小写, A5 H6 ?2 a: Q! k4 B+ X9 Z$ C
2、域名跳转' Z5 l X4 C2 r1 X! W% e
如果请求为old.xp6.org下的所有URL,跳转至b.
; E" @8 u) [& C3 Q4 n3 B- RewriteEngine on, H8 e* h* a4 `) P! t
- #开启Rewrite模块
% ]) [0 u& C4 r1 R- c" E" ? - RewriteCond %{REMOTE_HOST} ^old.xp6.org$ [NC]
/ u8 g7 K3 G3 N, T3 ` - #针对host为old.xp6.org的主机做处理,^为开始字符,$为结尾字符
: W- |8 }4 {. c2 l- K4 f - RewriteRule (.*) http://b.xp6.org/$1 [R=301,L,NC]
复制代码 3、防盗链1 ]' Q% @( w+ N# h1 y
如果本网站的图片不想让其它网站调用,可以在 .htaccess或者apche的配置文件httpd.conf文件中添加以下内容
, t& \# l+ B" @- w- RewriteEngine on& [% O) _0 G3 T7 N/ g( Y6 N
- #开启Rewrite模块
8 O* X' v2 I4 q - RewriteCond %{HTTP_REFERER} !^$2 d) {- e# V. j# ~
- #如果不是直接输入图片地址! {& M5 P' t! t! O
- RewriteCond %{HTTP_REFERER} !img.xp6.org$ [NC]2 p' j, _0 r. j& \9 e
- #且如果不是img.xp6.org所有子域名调用的
8 R; q4 f" w% Q* z/ m - RewriteCond %{HTTP_REFERER} !img.xp6.org/(.*)$ [NC]" ~2 N- H5 X. S; e2 U4 S
- RewriteCond %{HTTP_REFERER} !qq.com [NC]
! F9 E- E6 V T' ^: n - RewriteCond %{HTTP_REFERER} !google.com [NC]! P" E# I, E) p7 D
- RewriteCond %{HTTP_REFERER} !google.cn [NC]
6 u9 [& w; i; _$ R - RewriteCond %{HTTP_REFERER} !baidu.com [NC]3 s( p% g) y% B/ V9 K
- RewriteCond %{HTTP_REFERER} !feedsky.com [NC]8 u& W% Z, J1 N1 ?9 \) \ q! 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文件
9 @5 J; ]6 V# t! Z, s在Apache2\conf\httpd.conf 最后一行添加. b3 n5 D9 }5 {% e
- RewriteEngine On; T! k& ]: P7 o
- RewriteRule ^(.*)-htm-(.*)$ $1.php?$2
复制代码 重启Apache,登陆后台开启全伪8 K5 E$ u- m3 F9 [ V, k
( `& ^4 G! ^" o6 E( p' S3 fLinux+Apache环境配置类似。( C3 |/ S, M0 R5 ], |" c; K+ ?
2 ?* ~ R: Q( S1 ^ `- B |
|