apache rewrite在per-server和per-dir两种模式下的效率对比[转]

虽说rewrite不 难,但有些细节可能不是每个人都清楚,比如说rewrite在per-server和per-dir两种配置下的效率,下面我们通过开启日志来判断到底哪 个效率好,在实验前请下做好相关配置,并编辑适当的测试文件a.html,b.html,每次测试后通过tail -f /usr/local/apache2/logs/rewrite.log来实时监控发生了什么。

先看per-server配置:

01 <VirtualHost *:80>
02     ServerName _default_
03
04     RewriteEngine on
05
06     RewriteLog /usr/local/apache2/logs/rewrite.log
07     RewriteLogLevel 9
08
09     # RewriteRule ^/a.html$ /usr/local/apache2/htdocs/b.html [L]
10     # RewriteRule ^/a.html$ /b.html
[L]
11 </VirtualHost>

规则:RewriteRule ^/a.html$ /usr/local/apache2/htdocs/b.html

日志:

init rewrite engine with requested uri /a.html
applying pattern ‘^/a.html

to uri ‘/a.html’
rewrite ‘/a.html’ -> ‘/usr/local/apache2/htdocs/b.html’
local path result: /usr/local/apache2/htdocs/b.html
go-ahead with /usr/local/apache2/htdocs/b.html [OK]

规则:RewriteRule ^/a.html$ /b.html

日志:

init rewrite engine with requested uri /a.html
applying pattern ‘^/a.html

to uri ‘/a.html’
rewrite ‘/a.html’ -> ‘/b.html’
local path result: /b.html
prefixed with document_root to /usr/local/apache2/htdocs/b.html
go-ahead with /usr/local/apache2/htdocs/b.html [OK]

再看per-dir配置:

01 <Directory “/usr/local/apache2/htdocs”>
02     RewriteEngine on
03
04     RewriteLog
/usr/local/apache2/logs/rewrite.log
05     RewriteLogLevel 9
06
07     # RewriteRule ^a.html$ /usr/local/apache2/htdocs/b.html
[L]
08     # RewriteRule ^a.html$ /b.html [L]
09     # RewriteRule ^a.html$ b.html [L]
10 </Directory>

规则:RewriteRule ^a.html$ /usr/local/apache2/htdocs/b.html

日志:

strip per-dir prefix: /usr/local/apache2/htdocs/a.html -> a.html
applying pattern ‘^a.html

to uri ‘a.html’
rewrite ‘a.html’ -> ‘/usr/local/apache2/htdocs/b.html’
strip document_root prefix: /usr/local/apache2/htdocs/b.html -> /b.html
internal redirect with /b.html [INTERNAL REDIRECT]
strip per-dir prefix: /usr/local/apache2/htdocs/b.html -> b.html
applying pattern ‘^a.html

to uri ‘b.html’
pass through /usr/local/apache2/htdocs/b.html

规则:RewriteRule ^a.html$ /b.html

日志:

strip per-dir prefix: /usr/local/apache2/htdocs/a.html -> a.html
applying pattern ‘^a.html

to uri ‘a.html’
rewrite ‘a.html’ -> ‘/b.html’
internal redirect with /b.html [INTERNAL REDIRECT]
strip per-dir prefix: /usr/local/apache2/htdocs/b.html -> b.html
applying pattern ‘^a.html

to uri ‘b.html’
pass through /usr/local/apache2/htdocs/b.html

规则:RewriteRule ^a.html$ b.html

日志:

strip per-dir prefix: /usr/local/apache2/htdocs/a.html -> a.html
applying pattern ‘^a.html

to uri ‘a.html’
rewrite ‘a.html’ -> ‘b.html’
add per-dir prefix: b.html -> /usr/local/apache2/htdocs/b.html
strip document_root prefix: /usr/local/apache2/htdocs/b.html -> /b.html
internal redirect with /b.html [INTERNAL REDIRECT]
strip per-dir prefix: /usr/local/apache2/htdocs/b.html -> b.html
applying pattern ‘^a.html

to uri ‘b.html’
pass through /usr/local/apache2/htdocs/b.html

即便简单通过日志的行数,我们也能看到per-server的效率高于per-dir,仔细查看日志,会发现当在per-dir配置下使用rewrite 时,系统会发生一次“INTERNAL REDIRECT”,所以以后写rewrite时,除非必要,否则不要使用pre-dir配置,而应尽可能使用per-server配置,至于是使用 file路径,还是url路径,差别不大,不过url路径的看上去更短些,也更好维护一些。

原文出自:http://hi.baidu.com/thinkinginlamp/blog/item/ff49972b9d267bf1e6cd4019.html