|
环境:系统 Windows+Apache 2.27 y" e g2 c- X2 {! g; T: V0 R
4 r$ y$ l/ M4 m* @! E7 R加载Rewrite模块:
. n+ C z1 R" ?6 i在conf目录下httpd.conf中找到 LoadModule rewrite_module modules/mod_rewrite.so 这句,去掉前边的注释符号“#”,或添加这句。
- L& w* R6 y* V: v, n5 I% z8 p1 s/ [* U# ^
允许在任何目录中使用“.htaccess”文件,将“AllowOverride”改成“All”(默认为“None”):1 q4 W( @% X) W" t& [
u& \5 E8 v3 L+ f( [( p( \0 Z
# AllowOverride controls what directives may be placed in .htaccess files.' [5 q; m7 r% T' }+ r4 ?
# It can be “All”, “None”, or any combination of the keywords:- Y; d, j" x) d3 K' r
# Options FileInfo AuthConfig Limit
5 x, I3 W/ t }: ]5 s#' Z! z0 L1 c2 d6 c7 ^; \
AllowOverride All
" r7 l% B& F7 H! l# i
; e1 J5 q$ N6 k) G% W* w. a在Windows系统下不能直接创建“.htaccess”文件,可以在命令行下使用“echo a> .htaccess”建立,然后使用记事本编辑。或者,系统设置显示扩展文件名,打开记事本另存为.htaccess文件,文件类型,所有文件。
5 s4 f: u! K! X$ Q9 z: I' h
- m0 ~( `1 [; r- W2 g# W" eApache Rewrite模块的简单应用:0 ]" G, A+ I! |) F5 d/ k
Rewrite的所有判断规则均基于Perl风格的正则表达式,通过以下基础示例能写出符合自己跳转需求的代码。4 q- J+ S: J5 {+ M% r; R
7 N: X, M" Z8 R# f3 S+ B! a# k: [
1、请求跳转& \# ~, D+ `6 o) ?7 U9 @2 o
目的是如果请求为.jsp文件,则跳转至其它域名访问。% k" J3 e+ G- A
例如:访问www.xp6.org/a.php跳转至b.xp6.org/b.php网页,访问www.xp6.org/news/index.php跳转至b.xp6.org/news/index.php网页. R2 O$ m* d9 n# Y7 ]' M
注意:不是使用HTML技术中的meta或者javascript方式,因为www.xp6.org/a.php这个文件并不存在,用的是Apache2.2服务器中的Rewrite模块。
$ t' C0 n% L7 r修改 .htaccess或apche的配置文件httpd.conf文件,添加以下内容0 u. M& m/ X4 L# t& l3 F% j8 K
- RewriteEngine on
0 i3 o9 |# X# S3 M2 P2 E - #开启Rewrite模块
8 i1 V2 b2 R T# M0 { - RewriteRule (.*)\.php$ http://www.xp6.org/$1\.jsp [R=301,L,NC]
复制代码 #截获所有.jsp请求,跳转到http://b.xp6.org/加上原来的请求再加上.php。R=301为301跳转,L为rewrite规则到此终止,NC为不区分大小写
# S4 }) [% `3 T& f8 K- D9 H5 f0 ]2、域名跳转
# x: ]% C4 K: h$ f, J: {8 M如果请求为old.xp6.org下的所有URL,跳转至b.
5 D% U9 G! a' p' J$ c5 Q" e: w5 z- RewriteEngine on
; c/ u; s4 F$ C - #开启Rewrite模块" m% E* W, O% P- g* ~ x+ n
- RewriteCond %{REMOTE_HOST} ^old.xp6.org$ [NC]8 }6 Y: V# @5 ~' |+ X8 P
- #针对host为old.xp6.org的主机做处理,^为开始字符,$为结尾字符
9 V+ h0 g- [. s# {! c! | - RewriteRule (.*) http://b.xp6.org/$1 [R=301,L,NC]
复制代码 3、防盗链
: I; Y: T6 G& r, h5 y如果本网站的图片不想让其它网站调用,可以在 .htaccess或者apche的配置文件httpd.conf文件中添加以下内容
: j* X3 x; ^6 F9 o! C9 }* a- RewriteEngine on
3 g/ ]1 V# E |8 z( z0 U6 F) N - #开启Rewrite模块
{7 X+ \7 K1 h$ _' [. p - RewriteCond %{HTTP_REFERER} !^$% R' S& I3 C1 F; R
- #如果不是直接输入图片地址% `6 s E- h! z$ g/ E
- RewriteCond %{HTTP_REFERER} !img.xp6.org$ [NC]
" x) u7 O, P& |! w - #且如果不是img.xp6.org所有子域名调用的8 Q" }0 C ?' E, J% M
- RewriteCond %{HTTP_REFERER} !img.xp6.org/(.*)$ [NC]
( J Y; R) z5 d- j7 A5 Q - RewriteCond %{HTTP_REFERER} !qq.com [NC]; K8 D& ^( u! v" Z
- RewriteCond %{HTTP_REFERER} !google.com [NC]
1 J R6 C+ a' v+ ^/ m+ K - RewriteCond %{HTTP_REFERER} !google.cn [NC]. [! z, n, a- T7 N
- RewriteCond %{HTTP_REFERER} !baidu.com [NC]
" c: ~ z7 ~5 s - RewriteCond %{HTTP_REFERER} !feedsky.com [NC]
) @8 @/ L9 Z7 t+ l, U - 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文件" b6 ~. D8 F. C( u* p0 y/ N* Y8 a
在Apache2\conf\httpd.conf 最后一行添加/ M. J: i! \8 u9 d) _! `6 q
- RewriteEngine On- b1 i( ] C. r1 s
- RewriteRule ^(.*)-htm-(.*)$ $1.php?$2
复制代码 重启Apache,登陆后台开启全伪
& ]" {6 V6 W* T% o# b) |' l$ \/ B7 y/ E
Linux+Apache环境配置类似。& a. m% }' g4 Y! ^! ?
9 ^0 F2 W) f% e
|
|