|
|
环境:系统 Windows+Apache 2.20 G* ]6 `; @* ]& s! Z" Y ]
+ y9 u0 H6 R' k& O加载Rewrite模块:
3 D$ t. m' y& M在conf目录下httpd.conf中找到 LoadModule rewrite_module modules/mod_rewrite.so 这句,去掉前边的注释符号“#”,或添加这句。# P: `. F7 p- g; L/ [" _0 w1 @
5 A$ |( ?9 a) i2 {- O
允许在任何目录中使用“.htaccess”文件,将“AllowOverride”改成“All”(默认为“None”):! o6 V: B1 Q y! _/ U. p
' B) ~; J3 G3 y% h% I, d# AllowOverride controls what directives may be placed in .htaccess files.4 `$ L8 S4 j8 ~3 \1 O
# It can be “All”, “None”, or any combination of the keywords:
6 g' ]- k/ `; x) _! O; C" k( U8 D# Options FileInfo AuthConfig Limit' b4 k( m# A) W; ?( E& C& a
#
D! A$ o4 T9 EAllowOverride All# Q0 l" |- f- w: W
9 |7 g1 Z+ _, {) m1 L' q
在Windows系统下不能直接创建“.htaccess”文件,可以在命令行下使用“echo a> .htaccess”建立,然后使用记事本编辑。或者,系统设置显示扩展文件名,打开记事本另存为.htaccess文件,文件类型,所有文件。
5 h# R6 P1 A5 L6 O2 t9 {
- ]1 ` A9 S- M6 u/ ZApache Rewrite模块的简单应用:; m8 H' j' m- y( K! v4 d0 I
Rewrite的所有判断规则均基于Perl风格的正则表达式,通过以下基础示例能写出符合自己跳转需求的代码。3 _3 X1 P8 j1 y0 c4 @
4 _4 {' p! Y$ u1、请求跳转& y+ N4 G5 Y0 N2 d& i s
目的是如果请求为.jsp文件,则跳转至其它域名访问。
9 r* `0 `- n* P* C3 _例如:访问www.xp6.org/a.php跳转至b.xp6.org/b.php网页,访问www.xp6.org/news/index.php跳转至b.xp6.org/news/index.php网页5 ^/ S, p" [& `* z5 b" @% v) z
注意:不是使用HTML技术中的meta或者javascript方式,因为www.xp6.org/a.php这个文件并不存在,用的是Apache2.2服务器中的Rewrite模块。1 t. Z H' R# F$ ]
修改 .htaccess或apche的配置文件httpd.conf文件,添加以下内容/ y6 ?: j2 C5 d, s
- RewriteEngine on
, l7 t- R/ w9 J8 e0 p$ w& A5 q9 | - #开启Rewrite模块; z% C$ `9 k# g: I3 T0 `) w( K( 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为不区分大小写
. B+ T" b& `* h! a" i2、域名跳转
+ o: Y2 B ?# B8 W) C如果请求为old.xp6.org下的所有URL,跳转至b.
+ R, x1 p8 m% P7 X- RewriteEngine on
6 l& T* x" F3 ~ - #开启Rewrite模块
( T8 `5 P8 x: c - RewriteCond %{REMOTE_HOST} ^old.xp6.org$ [NC]* G8 f6 z* P& i
- #针对host为old.xp6.org的主机做处理,^为开始字符,$为结尾字符
" t) \. P& M6 z2 a# L+ x/ m: w% W+ } - RewriteRule (.*) http://b.xp6.org/$1 [R=301,L,NC]
复制代码 3、防盗链
! y/ s9 l- j7 V3 R如果本网站的图片不想让其它网站调用,可以在 .htaccess或者apche的配置文件httpd.conf文件中添加以下内容
# `# s* C6 T" g. Y; P' P7 W6 z- RewriteEngine on
' _! \ @& f2 l+ z" C* ~9 k - #开启Rewrite模块
# x* i, Q4 O6 h1 }% I2 j - RewriteCond %{HTTP_REFERER} !^$
% H6 U- H: _" E6 P - #如果不是直接输入图片地址- c* `9 C9 M5 J# p6 T* `
- RewriteCond %{HTTP_REFERER} !img.xp6.org$ [NC]
' B! D. K/ K+ G: c; T, | - #且如果不是img.xp6.org所有子域名调用的
- U+ u# L* S4 D7 ]6 K: c - RewriteCond %{HTTP_REFERER} !img.xp6.org/(.*)$ [NC]
/ v' }' n2 q6 S1 {# T6 e; e, P - RewriteCond %{HTTP_REFERER} !qq.com [NC]
# h+ U6 l8 Q4 ~' E% R6 G0 t - RewriteCond %{HTTP_REFERER} !google.com [NC]' i" [% t" A" g, W' h7 C
- RewriteCond %{HTTP_REFERER} !google.cn [NC]' \6 _' ~1 H0 R
- RewriteCond %{HTTP_REFERER} !baidu.com [NC]8 P% r1 q f( s5 M+ @* U" `2 {
- RewriteCond %{HTTP_REFERER} !feedsky.com [NC]
; V3 j; d( O/ U/ w - 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文件* w" S" M' N1 N- P }9 Z7 N0 V: \
在Apache2\conf\httpd.conf 最后一行添加
* d1 t3 m+ i Y) D* p; T" z, D- RewriteEngine On M! c$ _1 z2 }* C2 x5 }2 V
- RewriteRule ^(.*)-htm-(.*)$ $1.php?$2
复制代码 重启Apache,登陆后台开启全伪
}* ^; O. n; n# H
- ~; l+ o, g: m. R9 WLinux+Apache环境配置类似。 U2 T; Z: g2 ^- ^
: U; f; L$ e* H2 n) E3 j
|
|