|
|
环境:系统 Windows+Apache 2.23 ] F! x% n; f, O+ l' F
% |2 z! P4 q- r8 p加载Rewrite模块:
1 F& o0 p$ b/ c5 _: m+ M在conf目录下httpd.conf中找到 LoadModule rewrite_module modules/mod_rewrite.so 这句,去掉前边的注释符号“#”,或添加这句。
8 n* V( d* y0 r2 {* F: l( N* U5 P* L" N6 I7 x1 G5 i0 _
允许在任何目录中使用“.htaccess”文件,将“AllowOverride”改成“All”(默认为“None”):2 C3 ~* Q3 \( R2 A: v
" R; s9 h3 _$ {1 p' E
# AllowOverride controls what directives may be placed in .htaccess files.
3 h Q1 Z# K+ y) b# It can be “All”, “None”, or any combination of the keywords:; R+ P) T% j$ [; a* T9 J! N3 Y# w
# Options FileInfo AuthConfig Limit# N& D3 k6 w2 ]8 _" V1 [+ O8 ~' x* z1 d
#. o2 O" x6 w6 K
AllowOverride All" j$ F4 b# |, ?- Y7 J
& K" y& S' r- V在Windows系统下不能直接创建“.htaccess”文件,可以在命令行下使用“echo a> .htaccess”建立,然后使用记事本编辑。或者,系统设置显示扩展文件名,打开记事本另存为.htaccess文件,文件类型,所有文件。
& O/ N7 _- `( |
2 G& W" f% K7 PApache Rewrite模块的简单应用:
2 R. m, R0 f9 x/ I1 `$ ZRewrite的所有判断规则均基于Perl风格的正则表达式,通过以下基础示例能写出符合自己跳转需求的代码。
+ a/ Y: s" u2 V& x/ W% V f$ t& J* S! r" X1 R7 V
1、请求跳转
& V& d! O& V* i- V% `/ D目的是如果请求为.jsp文件,则跳转至其它域名访问。
; r/ i$ X. f4 j# ]8 R& G7 L8 w例如:访问www.xp6.org/a.php跳转至b.xp6.org/b.php网页,访问www.xp6.org/news/index.php跳转至b.xp6.org/news/index.php网页
$ ~: P" L3 ]* H6 p注意:不是使用HTML技术中的meta或者javascript方式,因为www.xp6.org/a.php这个文件并不存在,用的是Apache2.2服务器中的Rewrite模块。
( B! f4 n0 l% l* k0 j2 F修改 .htaccess或apche的配置文件httpd.conf文件,添加以下内容# O. {8 s! \0 w, g" u* ~
- RewriteEngine on" d( o, B& d; Y8 {3 U
- #开启Rewrite模块
- H( W( `0 K! K: } T! x - RewriteRule (.*)\.php$ http://www.xp6.org/$1\.jsp [R=301,L,NC]
复制代码 #截获所有.jsp请求,跳转到http://b.xp6.org/加上原来的请求再加上.php。R=301为301跳转,L为rewrite规则到此终止,NC为不区分大小写 m1 T3 A* A' ^% p# h( M- R; H G
2、域名跳转
0 P) s5 h, Z9 {0 l7 v1 _1 c, w1 G# x0 j# x如果请求为old.xp6.org下的所有URL,跳转至b.2 K" r' J/ l, ~! v, T, O% V4 o
- RewriteEngine on
9 L2 E# C( `% h/ D& o" D; j: i - #开启Rewrite模块
, o7 s5 n+ \0 M2 I+ i - RewriteCond %{REMOTE_HOST} ^old.xp6.org$ [NC]9 c0 ^: U7 Y, R9 X' `# E
- #针对host为old.xp6.org的主机做处理,^为开始字符,$为结尾字符
; m% O8 p4 g& [) n* b) M8 e7 M - RewriteRule (.*) http://b.xp6.org/$1 [R=301,L,NC]
复制代码 3、防盗链
$ W( z b4 b" `如果本网站的图片不想让其它网站调用,可以在 .htaccess或者apche的配置文件httpd.conf文件中添加以下内容( H$ r) w9 B; S4 a: u3 f: Z: b
- RewriteEngine on. [1 z1 ?! ]$ S; x) n6 w/ t4 S3 I
- #开启Rewrite模块
& L% W' h4 S& H" E$ C - RewriteCond %{HTTP_REFERER} !^$: c& t7 P% p2 Z$ s; t* `
- #如果不是直接输入图片地址
7 H: ~$ O- b1 Z( s; Q - RewriteCond %{HTTP_REFERER} !img.xp6.org$ [NC]
, D( j9 q% E+ l7 V7 n- w - #且如果不是img.xp6.org所有子域名调用的
, ^9 ?+ W/ E# W6 i( C - RewriteCond %{HTTP_REFERER} !img.xp6.org/(.*)$ [NC]- t U9 m! U I- g
- RewriteCond %{HTTP_REFERER} !qq.com [NC]
7 s: |, {" z" q( J8 C% l/ f2 e - RewriteCond %{HTTP_REFERER} !google.com [NC]5 F( m/ W) o z- B+ k) ~
- RewriteCond %{HTTP_REFERER} !google.cn [NC]
4 i0 D( X: K2 k( S7 w ~ - RewriteCond %{HTTP_REFERER} !baidu.com [NC]
: H% Z$ c0 E! C9 x& M - RewriteCond %{HTTP_REFERER} !feedsky.com [NC]
s9 @8 t2 g! s; Z% y7 O - 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文件$ p' \" v& i; f$ C% y' o% z( P* y
在Apache2\conf\httpd.conf 最后一行添加
9 K! u" v }; K1 @1 R/ @% D- RewriteEngine On. |% Q5 c' g, z8 k- E
- RewriteRule ^(.*)-htm-(.*)$ $1.php?$2
复制代码 重启Apache,登陆后台开启全伪; Y1 E+ W/ T3 P6 Q
% O# z S# h6 W6 U1 q- Z
Linux+Apache环境配置类似。
& Z! J: K& z0 d2 A0 w: r2 u9 ]& @9 G' k& a0 X
|
|