Apache的Mod_rewrite 例子(转)
转自dreamwaver的博客(http://dreamwaver.bokee.com/5692845.html)目标重写设置说明规范化URLRewriteRule ^/~([^/]+)/?(.*) /u/$1/$2 将/~user重写为/u/user的形式 RewriteRule ^/()/([^/]+)$ /$1/$2/ 将/u/user末尾漏掉的/补上 规范化HostNameRewriteCond %{HTTP_HOST} !^fully\.qualified\.domain\.name 域名不合格 RewriteCond %{HTTP_HOST} !^$不空 RewriteCond %{SERVER_PORT} !^80$不是80端口 RewriteRule ^/(.*) http://fully.qualified.domain.name:%{SERVER_PORT}/$1 重写 RewriteCond %{HTTP_HOST} !^fully\.qualified\.domain\.name RewriteCond %{HTTP_HOST} !^$ RewriteRule ^/(.*) http://fully.qualified.domain.name/$1 URL根目录转移RewriteEngine on RewriteRule ^/$ /e/www/ 从/移到/e/www/ 末尾目录补斜线RewriteEngine on (目录范围内)RewriteBase /~quux/ RewriteRule ^foo$ foo/ /~quux/foo是一个目录,补/ RewriteEngine on RewriteBase /~quux/ RewriteCond %{REQUEST_FILENAME} -d如果请文件名是个目录 RewriteRule ^(.+[^/])$ $1/ 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 ^/()/([^/]+)/?$ /$1/$2/.www/ RewriteRule ^/()/([^/]+)/([^.]+.+) /$1/$2/.www/$3\ URL根目录搬迁RewriteEngine on RewriteRule ^/~(.+) http://newserver/~$1 到其它服务器 所用户名首字母分RewriteEngine on RewriteRule ^/~(()+)(.*) /home/$2/$1/.www$3 内一层括号为$2 NCSA imagemap移RewriteEngine on 植为mod_imapRewriteRule ^/cgi-bin/imagemap(.*) $1 多目录查找资源RewriteEngine on # first try to find it in custom/... RewriteCond /your/docroot/dir1/%{REQUEST_FILENAME} -f RewriteRule ^(.+) /your/docroot/dir1/$1 # second try to find it in pub/... RewriteCond /your/docroot/dir2/%{REQUEST_FILENAME} -f RewriteRule ^(.+) /your/docroot/dir2/$1 # else go on for other Alias or ScriptAlias directives, RewriteRule ^(.+) - 据URL设置环境变量RewriteEngine on RewriteRule ^(.*)/S=([^/]+)/(.*) $1/$3 虚拟主机RewriteEngine on RewriteCond %{HTTP_HOST} ^www\.[^.]+\.host\.com$基于用户名 RewriteRule ^(.+) %{HTTP_HOST}$1 RewriteRule ^www\.([^.]+)\.host\.com(.*) /home/$1$2 内外人有别RewriteEngine on RewriteCond %{REMOTE_HOST} !^.+\.ourdomain\.com$基于远程主机 RewriteRule ^(/~.+) http://www.somewhere.com/$1 错误重定向RewriteEngine on RewriteCond /your/docroot/%{REQUEST_FILENAME} !-f不是regular文件 RewriteRule ^(.+) http://webserverB.dom/$1 程序处理特殊协议RewriteRule ^xredirect:(.+) /path/to/nph-xredirect.cgi/$1 \Xredirect协议 最近镜像下载RewriteEngine on RewriteMap multiplex txt:/path/to/map.cxan顶级域名与最近ftp服务器映射 RewriteRule ^/CxAN/(.*) %{REMOTE_HOST}::$1 RewriteRule ^.+\.(+)::(.*)$ ${multiplex:$1|ftp.default.dom}$2 据顶级域名不同提供不同的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 # rewrite to document.phtml if exists RewriteCond %{REQUEST_FILENAME}.phtml -f如果存在$1.phtml则重写 RewriteRule ^(.*)$ $1.phtml # 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 据浏览器类型重写RewriteCond %{HTTP_USER_AGENT} ^Mozilla/3.* RewriteRule ^foo\.html$ foo.NS.html RewriteCond %{HTTP_USER_AGENT} ^Lynx/.* RewriteCond %{HTTP_USER_AGENT} ^Mozilla/.* RewriteRule ^foo\.html$ foo.20.html RewriteRule ^foo\.html$ foo.32.html 动态镜像远程资源RewriteEngine on RewriteBase /~quux/ RewriteRule ^hotsheet/(.*)$ http://www.tstimpreso.com/hotsheet/$1 利用了代理模块 RewriteEngine on RewriteBase /~quux/ RewriteRule ^usa-news\.html$ http://www.quux-corp.com/news/index.html 反向动态镜像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} #!/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 阻击机器人RewriteCond %{HTTP_USER_AGENT} ^NameOfBadRobot.* RewriteCond %{REMOTE_ADDR} ^123\.45\.67\.$ RewriteRule ^/~quux/foo/arc/.+ - 阻止盗连你的图片RewriteCond %{HTTP_REFERER} !^$ RewriteCond %{HTTP_REFERER} !^http://www.quux-corp.de/~quux/.*$ 自己的连接可不能被阻止 RewriteRule .*\.gif$ - RewriteCond %{HTTP_REFERER} !^$ RewriteCond %{HTTP_REFERER} !.*/foo-with-gif\.html$ RewriteRule ^inlined-in-foo\.gif$ - 拒绝某些主机访问RewriteEngine on RewriteMap hosts-deny txt:/path/to/hosts.deny RewriteCond ${hosts-deny:%{REMOTE_HOST}|NOT-FOUND} !=NOT-FOUND RewriteCond ${hosts-deny:%{REMOTE_ADDR}|NOT-FOUND} !=NOT-FOUND RewriteRule ^/.* - 用户授权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/ - 外部重写程序模板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/(+)$ /content.php?id=$1 RewriteRule ^/products/(+),(*),({0,3}),(*),(*$) /marso/content.php?id=$1&sort=$2&order=$3&start=$4
页:
[1]