apache rewrite

用rewrite可實現的部分:URL根目錄搬遷,多目錄查找資源,阻止盜連你的圖片,拒絕某些主機訪問,基於時間重寫,據瀏覽器類型重寫,動態鏡像遠程資源,外部重寫程序模板,等等php

詳見下表:
html

目標 重寫設置 說明
規範化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
相關文章
相關標籤/搜索