Apache.Rewrite.拟静态配置与规则学习

一、mod_rewrite 简介和配置 1、Rewirte主要的功能就是实现URL的跳转和隐藏真实地址,基于Perl语言的正则 表达式规范。平时帮助我们实现拟静态,拟目录,域名跳转,防止盗链等 2、Apache配置: 支持《httpd.conf 配置》和目录《 .htaccess配置》 1)# LoadModule rewrite_module modules/mod_rewrite.so 去除前面的# 2)AllowOverride None 修改为: AllowOverride All (把AllowOverride None全部改吧) 3)重启Apache服务器 4)以上三步配置就差不多行了,当然还有配置.htacess文件名的地方,一般默认为.htaccess (遇到.htaccess不生效,一般是规则写错了,今天就是以为.htaccess不起作用,检查到最后原来是把规则写错了,烦了一个下午) 二、对rewrite进行测试 在.htaccess文件中写如下测试: RewriteEngine on Rewriterule ^t_(.*).html$ test.php?text2 可选择的字符串:text1或text2 ? 匹配0到1个字符 * 匹配0到多个字符 + 匹配1到多个字符 ^ 字符串开始标志 $ 字符串结束标志 /n 转义符标志 反向引用 $N 用于 RewriteRule 中匹配的变量调用(0 <= N <= 9) 反向引用 %N 用于 RewriteCond 中最后一个匹配的变量调用(1 <= N <= 9) 4、RewriteCond标志符 'nocase|NC'(no case)忽略大小 'ornext|OR' (or next condition)逻辑或,可以同时匹配多个RewriteCond条件 5、RewriteRule适用的标志符 'redirect|R [=code]' (force redirect)强迫重写为基于http开头的外部转向(注意URL的变化) 如:[R=301,L] 'forbidden|F' (force URL to be forbidden)重写为禁止访问 'proxy|P' (force proxy)重写为通过代理访问的http路径 'last|L' (last rule)最后的重写规则标志,如果匹配,不再执行以后的规则 'next|N' (next round)循环同一个规则,直到不能满足匹配 'chain|C' (chained with next rule)如果匹配该规则,则继续下面的有Chain标志的规则。 'type|T=MIME-type' (force MIME type)指定MIME类型 'nosubreq|NS' (used only if no internal sub-request)如果是内部子请求则跳过 'nocase|NC' (no case)忽略大小 'qsappend|QSA' (query string append)附加查询字符串 'noescape|NE' (no URI escaping of output)禁止URL中的字符自动转义成%[0-9]+的形式。 'passthrough|PT' (pass through to next handler)将重写结果运用于mod_alias 'skip|S=num' (skip next rule(s))跳过下面几个规则 'env|E=VAR:VAL' (set environment variable)添加环境变量 6、实际操作 例子: RewriteEngine on RewriteCond %{HTTP_USER_AGENT} ^MSIE [NC,OR] RewriteCond %{HTTP_USER_AGENT} ^Opera [NC] RewriteRule ^.* - [F,L] 这里”-”表示没有替换,浏览器为IE和Opera的访客将被禁止访问。 例子: RewriteEngine On RewriteBase /test RewriteCond %{REQUEST_FILENAME}.php -f RewriteRule ([^/]+)$ /test/$1.php #for example: /test/admin => /test/admin.php RewriteRule ([^/]+)/.html$ /test/$1.php [L] #for example: /test/admin.html => /test/admin.php 限制目录只能显示图片 < IfModule mod_rewrite.c> RewriteEngine on RewriteCond %{REQUEST_FILENAME} !^.*/.(gif|jpg|jpeg|png|swf)$ RewriteRule .*$ - [F,L] < /IfModule> 四、更多例子集合 在 httpd 中将一个域名转发到另一个域名 虚拟主机世界近期更换了域名,新域名为 www.wbhw.com, 更加简短好记。这时需要将原来的域名 webhosting-world.com, 以及论坛所在地址 webhosting-world.com/forums/ 定向到新的域名,以便用户可以找到,并且使原来的论坛 URL 继续有效而不出现 404 未找到,比如原来的 http://www. webhosting-world.com/forums/-f60.html, 让它在新的域名下继续有效,点击后转发到 http://bbs.wbhw.com/-f60.html, 这就需要用 apache 的 Mod_rewrite 功能来实现。 在< virtualhost> 中添加下面的重定向规则: RewriteEngine On # Redirect webhosting-world.com/forums to bbs.wbhw.com RewriteCond %{REQUEST_URI} ^/forums/ RewriteRule /forums/(.*) http://bbs.wbhw.com/$1 [R=permanent,L] # Redirect webhosting-world.com to wbhw.com RewriteCond %{REQUEST_URI} !^/forums/ RewriteRule /(.*) http://www.wbhw.com/$1 [R=permanent,L] 添加了上面的规则以后, 里的全部内容如下: < virtualhost *:80> ServerAlias webhosting-world.com ServerAdmin admin@webhosting-world.com DocumentRoot /path/to/webhosting-world/root ServerName www.webhosting-world.com RewriteEngine On # Redirect webhosting-world.com/forums to bbs.wbhw.com RewriteCond %{REQUEST_URI} ^/forums/ RewriteRule /forums/(.*) http://bbs.wbhw.com/$1 [R=permanent,L] # Redirect webhosting-world.com to wbhw.com RewriteCond %{REQUEST_URI} !^/forums/ RewriteRule /(.*) http://www.wbhw.com/$1 [R=permanent,L] < /virtualhost> URL重定向例子一: 1.http://www.zzz.com/xxx.php-> http://www.zzz.com/xxx/ 2.http://yyy.zzz.com-> http://www.zzz.com/user.php?username=yyy 的功能 RewriteEngine On RewriteCond %{HTTP_HOST} ^www.zzz.com RewriteCond %{REQUEST_URI} !^user\.php$ RewriteCond %{REQUEST_URI} \.php$ RewriteRule (.*)\.php$ http://www.zzz.com/$1/ [R] RewriteCond %{HTTP_HOST} !^www.zzz.com RewriteRule ^(.+) %{HTTP_HOST} [C] RewriteRule ^([^\.]+)\.zzz\.com http://www.zzz.com/user.php?username=$1 例子二: /type.php?typeid=* --> /type*.html /type.php?typeid=*&page=* --> /type*page*.html RewriteRule ^/type([0-9]+).html$ /type.php?typeid=$1 [PT] RewriteRule ^/type([0-9]+)page([0-9]+).html$ /type.php?typeid=$1&page=$2 [PT]