2013年4月3日星期三

Apache Rewrite Rules How To

It is not a funny topic, isn't it !
I have read a blog http://smart-scripts-for-sysadmins.blogspot.in/2012/07/apache-url-redirection-cheat-sheet.html and download the pdf that in it , I saw it , but I can not all understand what the pdf say, so this blog's aim is to know the basic fundamental of apache rewrite .

Example:

RewriteEngine On
RewriteLogLevel 4
Rewritecond %{REQUEST_URI} !^/HTML2/   // 如果uri不是 HTML2
Rewritecond %{REQUEST_URI} !munin$ // 如果uri不是 munin
Rewritecond %{REQUEST_URI} !munin2$ // 如果uri不是munin2
RewriteRule ^/(.*)$ /HTML2/$1 [R,L] // rewrite redirect last
Rewritelog /var/log/apache2/rewrite

如果我访问 http://42.96.166.148/a 那么 rewrite日志是:

116.251.209.180 - - [03/Apr/2013:10:17:07 +0800] [42.96.166.148/sid#7fc645824860][rid#7fc645b01428/initial] (2) init rewrite engine with requested uri /a
116.251.209.180 - - [03/Apr/2013:10:17:07 +0800] [42.96.166.148/sid#7fc645824860][rid#7fc645b01428/initial] (3) applying pattern '^/(.*)$' to uri '/a' // Here, apache is using the pattern to get the uri %{REQUEST_URI} 
116.251.209.180 - - [03/Apr/2013:10:17:07 +0800] [42.96.166.148/sid#7fc645824860][rid#7fc645b01428/initial] (4) RewriteCond: input='/a' pattern='!^/HTML2/' => matched
116.251.209.180 - - [03/Apr/2013:10:17:07 +0800] [42.96.166.148/sid#7fc645824860][rid#7fc645b01428/initial] (4) RewriteCond: input='/a' pattern='!munin$' => matched
116.251.209.180 - - [03/Apr/2013:10:17:07 +0800] [42.96.166.148/sid#7fc645824860][rid#7fc645b01428/initial] (4) RewriteCond: input='/a' pattern='!munin2$' => matched
116.251.209.180 - - [03/Apr/2013:10:17:07 +0800] [42.96.166.148/sid#7fc645824860][rid#7fc645b01428/initial] (2) rewrite '/a' -> '/HTML2/a'
116.251.209.180 - - [03/Apr/2013:10:17:07 +0800] [42.96.166.148/sid#7fc645824860][rid#7fc645b01428/initial] (2) explicitly forcing redirect with http://42.96.166.148/HTML2/a
116.251.209.180 - - [03/Apr/2013:10:17:07 +0800] [42.96.166.148/sid#7fc645824860][rid#7fc645b01428/initial] (1) escaping http://42.96.166.148/HTML2/a for redirect
116.251.209.180 - - [03/Apr/2013:10:17:07 +0800] [42.96.166.148/sid#7fc645824860][rid#7fc645b01428/initial] (1) redirect to http://42.96.166.148/HTML2/a [REDIRECT/302]
116.251.209.180 - - [03/Apr/2013:10:17:08 +0800] [42.96.166.148/sid#7fc645824860][rid#7fc645b01428/initial] (2) init rewrite engine with requested uri /HTML2/a
116.251.209.180 - - [03/Apr/2013:10:17:08 +0800] [42.96.166.148/sid#7fc645824860][rid#7fc645b01428/initial] (3) applying pattern '^/(.*)$' to uri '/HTML2/a'
116.251.209.180 - - [03/Apr/2013:10:17:08 +0800] [42.96.166.148/sid#7fc645824860][rid#7fc645b01428/initial] (4) RewriteCond: input='/HTML2/a' pattern='!^/HTML2/' => not-matched
116.251.209.180 - - [03/Apr/2013:10:17:08 +0800] [42.96.166.148/sid#7fc645824860][rid#7fc645b01428/initial] (1) pass through /HTML2/a
116.251.209.180 - - [03/Apr/2013:10:17:08 +0800] [42.96.166.148/sid#7fc645824860][rid#7fc645b01428/initial] (2) init rewrite engine with requested uri /favicon.ico
116.251.209.180 - - [03/Apr/2013:10:17:08 +0800] [42.96.166.148/sid#7fc645824860][rid#7fc645b01428/initial] (3) applying pattern '^/(.*)$' to uri '/favicon.ico'
116.251.209.180 - - [03/Apr/2013:10:17:08 +0800] [42.96.166.148/sid#7fc645824860][rid#7fc645b01428/initial] (4) RewriteCond: input='/favicon.ico' pattern='!^/HTML2/' => matched
116.251.209.180 - - [03/Apr/2013:10:17:08 +0800] [42.96.166.148/sid#7fc645824860][rid#7fc645b01428/initial] (4) RewriteCond: input='/favicon.ico' pattern='!munin$' => matched
116.251.209.180 - - [03/Apr/2013:10:17:08 +0800] [42.96.166.148/sid#7fc645824860][rid#7fc645b01428/initial] (4) RewriteCond: input='/favicon.ico' pattern='!munin2$' => matched
116.251.209.180 - - [03/Apr/2013:10:17:08 +0800] [42.96.166.148/sid#7fc645824860][rid#7fc645b01428/initial] (2) rewrite '/favicon.ico' -> '/HTML2/favicon.ico'
116.251.209.180 - - [03/Apr/2013:10:17:08 +0800] [42.96.166.148/sid#7fc645824860][rid#7fc645b01428/initial] (2) explicitly forcing redirect with http://42.96.166.148/HTML2/favicon.ico
116.251.209.180 - - [03/Apr/2013:10:17:08 +0800] [42.96.166.148/sid#7fc645824860][rid#7fc645b01428/initial] (1) escaping http://42.96.166.148/HTML2/favicon.ico for redirect
116.251.209.180 - - [03/Apr/2013:10:17:08 +0800] [42.96.166.148/sid#7fc645824860][rid#7fc645b01428/initial] (1) redirect to http://42.96.166.148/HTML2/favicon.ico [REDIRECT/302]
116.251.209.180 - - [03/Apr/2013:10:17:09 +0800] [42.96.166.148/sid#7fc645824860][rid#7fc645b01428/initial] (2) init rewrite engine with requested uri /HTML2/favicon.ico
116.251.209.180 - - [03/Apr/2013:10:17:09 +0800] [42.96.166.148/sid#7fc645824860][rid#7fc645b01428/initial] (3) applying pattern '^/(.*)$' to uri '/HTML2/favicon.ico'
116.251.209.180 - - [03/Apr/2013:10:17:09 +0800] [42.96.166.148/sid#7fc645824860][rid#7fc645b01428/initial] (4) RewriteCond: input='/HTML2/favicon.ico' pattern='!^/HTML2/' => not-matched
116.251.209.180 - - [03/Apr/2013:10:17:09 +0800] [42.96.166.148/sid#7fc645824860][rid#7fc645b01428/initial] (1) pass through /HTML2/favicon.ico

# How to redirect users to the identify web pages based on their web browser?

# MS Internet Explorer - Mozilla v4
RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} ^Mozilla/4(.*)MSIE
RewriteRule ^index\.html$ /index.IE.html [L]

没有评论:

发表评论