用rewrite可实现的部分:URL根目录搬迁,多目录查找资源,阻止盗连你的图片,拒绝某些主机访问,基于时间重写,据浏览器类型重写,动态镜像远程资源,外部重写程序模板,等等

详见下表:

目标 重写设置 说明
规范化URL RewriteRule ^/~([^/]+)/?(.*) /u/$1/$2 [R] 将/~user重写为/u/user的形式
  RewriteRule ^/([uge])/([^/]+)$ /$1/$2/ [R] 将/u/user末尾漏掉的/补上
     
规范化HostName RewriteCond %{HTTP_HOST} !^fully\.qualified\.domain\.name [NC] 域名不合格
  RewriteCond %{HTTP_HOST} !^$ 不空
  RewriteCond %{SERVER_PORT} !^80$ 不是80端口
  RewriteRule ^/(.*) http://fully.qualified.domain.name:%{SERVER_PORT}/$1 [L,R] 重写
  RewriteCond %{HTTP_HOST} !^fully\.qualified\.domain\.name [NC]  
  RewriteCond %{HTTP_HOST} !^$  
  RewriteRule ^/(.*) http://fully.qualified.domain.name/$1 [L,R]  
     
URL根目录转移 RewriteEngine on  
  RewriteRule ^/$ /e/www/ [R] 从/移到/e/www/
     
末尾目录补斜线 RewriteEngine on  
(目录范围内) RewriteBase /~quux/  
  RewriteRule ^foo$ foo/ [R] /~quux/foo是一个目录,补/
     
  RewriteEngine on  
  RewriteBase /~quux/  
  RewriteCond %{REQUEST_FILENAME} -d 如果请文件名是个目录
  RewriteRule ^(.+[^/])$ $1/ [R] URL末尾不是斜线时补上
     
Web集群 RewriteEngine on  
  RewriteMap user-to-host txt:/path/to/map.user-to-host 用户-服务器映射
  RewriteMap group-to-host txt:/path/to/map.group-to-host 组-服务器映射
  RewriteMap entity-to-host txt:/path/to/map.entity-to-host 实体-服务器映射
  RewriteRule ^/u/([^/]+)/?(.*) http://${user-to-host:$1|server0}/u/$1/$2 用户均衡
  RewriteRule ^/g/([^/]+)/?(.*) http://${group-to-host:$1|server0}/g/$1/$2 组均衡
  RewriteRule ^/e/([^/]+)/?(.*) http://${entity-to-host:$1|server0}/e/$1/$2 实体均衡
  RewriteRule ^/([uge])/([^/]+)/?$ /$1/$2/.www/  
  RewriteRule ^/([uge])/([^/]+)/([^.]+.+) /$1/$2/.www/$3\  
     
URL根目录搬迁 RewriteEngine on  
  RewriteRule ^/~(.+) http://newserver/~$1 [R,L] 到其它服务器
     
所用户名首字母分 RewriteEngine on  
  RewriteRule ^/~(([a-z])[a-z0-9]+)(.*) /home/$2/$1/.www$3 内一层括号为$2
     
NCSA imagemap移 RewriteEngine on  
植为mod_imap RewriteRule ^/cgi-bin/imagemap(.*) $1 [PT]  
     
多目录查找资源 RewriteEngine on  
  # first try to find it in custom/…  
  RewriteCond /your/docroot/dir1/%{REQUEST_FILENAME} -f  
  RewriteRule ^(.+) /your/docroot/dir1/$1 [L]  
  # second try to find it in pub/…  
  RewriteCond /your/docroot/dir2/%{REQUEST_FILENAME} -f  
  RewriteRule ^(.+) /your/docroot/dir2/$1 [L]  
  # else go on for other Alias or Alias directives,  
  RewriteRule ^(.+) – [PT]  
     
据URL设置环境变量 RewriteEngine on  
  RewriteRule ^(.*)/S=([^/]+)/(.*) $1/$3 [E=STATUS:$2]  
     
虚拟主机 RewriteEngine on  
  RewriteCond %{HTTP_HOST} ^www\.[^.]+\.host\.com$ 基于用户名
  RewriteRule ^(.+) %{HTTP_HOST}$1 [C]  
  RewriteRule ^www\.([^.]+)\.host\.com(.*) /home/$1$2  
     
内外人有别 RewriteEngine on  
  RewriteCond %{REMOTE_HOST} !^.+\.ourdomain\.com$ 基于远程主机
  RewriteRule ^(/~.+) http://www.somewhere.com/$1 [R,L]  
     
错误重定向 RewriteEngine on  
  RewriteCond /your/docroot/%{REQUEST_FILENAME} !-f 不是regular文件
  RewriteRule ^(.+) http://webserverB.dom/$1  
     
程序处理特殊协议 RewriteRule ^xredirect:(.+) /path/to/nph-xredirect.cgi/$1 \ Xredirect协议
  [T=application/x-httpd-cgi,L]  
     
最近镜像下载 RewriteEngine on  
  RewriteMap multiplex txt:/path/to/map.cxan 顶级域名与最近ftp服务器映射
  RewriteRule ^/CxAN/(.*) %{REMOTE_HOST}::$1 [C]  
  RewriteRule ^.+\.([a-zA-Z]+)::(.*)$ ${multiplex:$1|ftp.default.dom}$2 [R,L] 据顶级域名不同提供不同的FTP服务器
     
基于时间重写 RewriteEngine on  
  RewriteCond %{TIME_HOUR}%{TIME_MIN} >0700  
  RewriteCond %{TIME_HOUR}%{TIME_MIN} <1900  
  RewriteRule ^foo\.html$ foo.day.html 白天为早晚7点间
  RewriteRule ^foo\.html$ foo.night.html 其余为夜间
     
向前兼容扩展名 RewriteEngine on  
  RewriteBase /~quux/  
  # parse out basename, but remember the fact  
  RewriteRule ^(.*)\.html$ $1 [C,E=WasHTML:yes]  
  # rewrite to phtml if exists  
  RewriteCond %{REQUEST_FILENAME}.phtml -f 如果存在$1.phtml则重写
  RewriteRule ^(.*)$ $1.phtml [S=1]  
  # else reverse the previous basename cutout  
  RewriteCond %{ENV:WasHTML} ^yes$ 如果不存在$1.phtml,则保持不变
  RewriteRule ^(.*)$ $1.html  
     
文件改名(目录级) RewriteEngine on 内部重写
  RewriteBase /~quux/  
  RewriteRule ^foo\.html$ bar.html  
     
  RewriteEngine on 重定向由客户端再次提交
  RewriteBase /~quux/  
  RewriteRule ^foo\.html$ bar.html [R]  
     
据浏览器类型重写 RewriteCond %{HTTP_USER_AGENT} ^Mozilla/3.*  
  RewriteRule ^foo\.html$ foo.NS.html [L]  
  RewriteCond %{HTTP_USER_AGENT} ^Lynx/.* [OR]  
  RewriteCond %{HTTP_USER_AGENT} ^Mozilla/[12].*  
  RewriteRule ^foo\.html$ foo.20.html [L]  
  RewriteRule ^foo\.html$ foo.32.html [L]  
     
动态镜像远程资源 RewriteEngine on  
  RewriteBase /~quux/  
  RewriteRule ^hotsheet/(.*)$ http://www.tstimpreso.com/hotsheet/$1 [P] 利用了代理模块
     
  RewriteEngine on  
  RewriteBase /~quux/  
  RewriteRule ^usa-news\.html$ http://www.quux-corp.com/news/index.html [P]  
     
反向动态镜像 RewriteEngine on  
  RewriteCond /mirror/of/remotesite/$1 -U  
  RewriteRule ^http://www\.remotesite\.com/(.*)$ /mirror/of/remotesite/$1  
     
负载均衡 RewriteEngine on 利用代理实现round-robin效果
  RewriteMap lb prg:/path/to/lb.pl  
  RewriteRule ^/(.+)$ ${lb:$1} [P,L]  
     
  #!/path/to/perl  
  $| = 1;  
  $name = “www”; # the hostname base  
  $first = 1; # the first server (not 0 here, because 0 is myself)  
  $last = 5; # the last server in the round-robin  
  $domain = “foo.dom”; # the domainname  
  $cnt = 0;  
  while (<STDIN>) {  
  $cnt = (($cnt+1) % ($last+1-$first));  
  $server = sprintf(“%s%d.%s”, $name, $cnt+$first, $domain);  
  print “http://$server/$_”;  
  }  
  ##EOF##  
     
静态页面变脚本 RewriteEngine on  
  RewriteBase /~quux/  
  RewriteRule ^foo\.html$ foo.cgi [T=application/x-httpd-cgi]  
     
阻击机器人 RewriteCond %{HTTP_USER_AGENT} ^NameOfBadRobot.*  
  RewriteCond %{REMOTE_ADDR} ^123\.45\.67\.[8-9]$  
  RewriteRule ^/~quux/foo/arc/.+ – [F]  
     
阻止盗连你的图片 RewriteCond %{HTTP_REFERER} !^$  
  RewriteCond %{HTTP_REFERER} !^http://www.quux-corp.de/~quux/.*$ [NC] 自己的连接可不能被阻止
  RewriteRule .*\.gif$ – [F]  
     
  RewriteCond %{HTTP_REFERER} !^$  
  RewriteCond %{HTTP_REFERER} !.*/foo-with-gif\.html$  
  RewriteRule ^inlined-in-foo\.gif$ – [F]  
     
拒绝某些主机访问 RewriteEngine on  
  RewriteMap hosts-deny txt:/path/to/hosts.deny  
  RewriteCond ${hosts-deny:%{REMOTE_HOST}|NOT-FOUND} !=NOT-FOUND [OR]  
  RewriteCond ${hosts-deny:%{REMOTE_ADDR}|NOT-FOUND} !=NOT-FOUND  
  RewriteRule ^/.* – [F]  
     
用户授权 RewriteCond %{REMOTE_IDENT}@%{REMOTE_HOST} !^friend1@client1.quux-corp\.com$  
  RewriteCond %{REMOTE_IDENT}@%{REMOTE_HOST} !^friend2@client2.quux-corp\.com$  
  RewriteCond %{REMOTE_IDENT}@%{REMOTE_HOST} !^friend3@client3.quux-corp\.com$  
  RewriteRule ^/~quux/only-for-friends/ – [F]  
     
外部重写程序模板 RewriteEngine on  
  RewriteMap quux-map prg:/path/to/map.quux.pl  
  RewriteRule ^/~quux/(.*)$ /~quux/${quux-map:$1}  
     
  #!/path/to/perl  
  $| = 1;  
  while (<>) {  
  s|^foo/|bar/|;  
  print $_;  
  }  
     
搜索引擎友好 RewriteRule ^/products$ /content.php  
  RewriteRule ^/products/([0-9]+)$ /content.php?id=$1  
  RewriteRule ^/products/([0-9]+),([ad]*),([0-9]{0,3}),([0-9]*),([0-9]*$) /marso/content.php?id=$1&sort=$2&order=$3&start=$4

转载请注明出处:http://www.onexin.net/?p=3114

Apache rewrite 详解的更多相关文章

  1. Nginx Rewrite详解

    Nginx Rewrite详解 引用链接:http://blog.cafeneko.info/2010/10/nginx_rewrite_note/ 原文如下: 在新主机的迁移过程中,最大的困难就是W ...

  2. (原创)LAMP搭建之二:apache配置文件详解(中英文对照版)

    LAMP搭建之二:apache配置文件详解(中英文对照版) # This is the main Apache server configuration file. It contains the # ...

  3. 转: Apache SSI详解及应用

    转: Apache SSI详解及应用 什么是 SSI? SSI(Server Side Includes),是嵌套在 HTML 网页中的指示语句,由后台服务器进行代码的解释计算.使用 SSI 可以动态 ...

  4. apache配置文件详解与优化

    apache配置文件详解与优化 一.总结 一句话总结:结合apache配置文件中的英文说明和配置详解一起看 1.apache模块配置用的什么标签? IfModule 例如: <IfModule ...

  5. Apache伪静态Rewrite详解

    一.Rewrite规则简介:Rewirte主要的功能就是实现URL的跳转,它的正则表达式是基于 Perl语言.可基于服务器级的(httpd.conf)和目录级的 (.htaccess)两种方式.如果要 ...

  6. Web性能压力测试工具之Apache AB 详解

    下载安装地址: http://httpd.apache.org/download.cgi yum install httpd-tools http://www.apachelounge.com/dow ...

  7. apache配置详解

    可参考:Apache 的 httpd.conf 详解 ServerTokens OS 此指令控制了Server回送给客户端的回应头域是否包含关于服务器OS类型和编译进的模块描述信息.服务器会发送:Se ...

  8. Apache日志详解

    在渗透测试的工作中,WEB网站的日志是非常重要的,今天总结了一些关于调配Apache日志的一些东西. 0x00 Apache日志文件名称及路径介绍 我们安装好Apache后,Apache的配置文件(h ...

  9. yum方式安装的Apache目录详解和配置说明

    在对httpd.conf文件进行解读之前,首先了解一下Redhat9中Apache服务器默认配置的一些基本信息:配置文件:/etc/httpd/conf/http.conf1)"/etc/h ...

随机推荐

  1. HDU 4394 Digital Square

    Digital Square Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  2. Iterator和ListIterator区别

    我们在使用List,Set的时候,为了实现对其数据的遍历,我们经常使用到了Iterator(迭代器).使用迭代器,你不需要干涉其遍历的过程,只需要每次取出一个你想要的数据进行处理就可以了. 但是在使用 ...

  3. 《深入Java虚拟机学习笔记》- 第4章 网络移动性

    Java虚拟机学习笔记(四)网络移动性

  4. 黑盒测试用例设计方法&理论结合实际 -> 因果图法

    一. 概念 因果图法是一种利用图解法分析输入的各种组合情况,从而设计测试用例的方法,它适合于检查程序输入条件的各种组合情况 二. 因果图法的应用 等价类划分法和边界值分析方法都是着重考虑输入条件,但没 ...

  5. Linux批量修改指定目录下的文件或文件夹权限

    在Puppet下很头大,尤其是文件拷贝,使用file的mode会导致文件或文件夹都一个权限. 暂时使用命令代替: 最近忙着明年的N多计划,待有空后继续研究.

  6. 2015上海网络赛 A Puzzled Elena

    题意:给定一棵树,求这个节点的所有子树中包括他本身与它互质的节点的个数. 解题思路:题利用dfs序+容斥原理+前缀和性质解决.题目中要求每个结点,和多少个它的子结点互素.如果每次为了求一个点去跑一遍d ...

  7. 寒假训练第九场 Brocard Point of a Triangle

    题意:求布洛卡点坐标 思路:直接利用布洛卡点的性质.http://pan.baidu.com/s/1eQiP76E #include<cstdio> #include<cstring ...

  8. uva11732 strcmp() Anyone?

    题意:给出多个字符串,两两配对,求总配对次数. 思路:如果两个字符串一样,ans=strlen(字符串)*2+2,如果不同,ans=公共前缀长度*2+1:用左儿子右兄弟建字典树.插入一个字符计算一次. ...

  9. CUDA网格限制

    如图

  10. Good practice release jar to Nexus

    Step  suppose you need to develop a feature,when you finish the feature ,you need to release the jar ...