诡异的apache RewriteCond %{REQUEST_FILENAME} !-s问题

原文出处:http://lufei-99999.blog.163.com/blog/static/748495420115845922988/

最近在做网站页面静态化这方面的工作,需要用到apache的配合。
网站静态化功能如下
当用户访问网站的index.html页面是,用apache判断index.html文件是否存在,如果不存在的话,转向index.do这个路径,
访问index.do路径是程序自动生成index.html文件,以后用户再次访问index.html文件时,直接访问index.html文件,实现网站自动静态化的功能。
但是在配置apache文件,及用apache判断文件是否存在时出现了问题。
http.conf文件添加
RewriteCond {REQUEST_FILENAME}   !-s
RewriteRule ^/a\.html$  /b.html   [L]
访问一直不对。就是访问a.html时,不管a.html文件是否存在,都访问b.html文件。
不过百度,还是google都搜索不到。
在根目录下面建立.htaccess文件
RrwriteBase /
RewriteCond {REQUEST_FILENAME}   !-s
RewriteRule ^/a\.html$  /b.html   [L]
访问文件时可以实现需要的功能。
百思不得其解了好几天。
突然想到开启apache的重写日志 看看
在http.conf文件添加
RewriteLog logs/rewrite.log
然后测试第一种情况
日志如下:
127.0.0.1 - - [08/Jun/2011:16:53:07 +0800] [www.a.com/sid#7b0770][rid#9e8030/initial] (2) init rewrite engine with requested uri /a.html
127.0.0.1 - - [08/Jun/2011:16:53:07 +0800] [www.a.com/sid#7b0770][rid#9e8030/initial] (3) applying pattern '^/a\.html$' to uri '/a.html'
127.0.0.1 - - [08/Jun/2011:16:53:07 +0800] [www.a.com/sid#7b0770][rid#9e8030/initial] (4) RewriteCond: input='/a.html' pattern='!-s' => matched
127.0.0.1 - - [08/Jun/2011:16:53:07 +0800] [www.a.com/sid#7b0770][rid#9e8030/initial] (2) rewrite '/a.html' -> '/b.html'

然后测试第二种情况
日志如下:
127.0.0.1 - - [08/Jun/2011:16:36:42 +0800] [www.a.com/sid#7b0688][rid#9e8030/initial] (2) init rewrite engine with requested uri /a.html
127.0.0.1 - - [08/Jun/2011:16:36:42 +0800] [www.a.com/sid#7b0688][rid#9e8030/initial] (1) pass through /a.html
127.0.0.1 - - [08/Jun/2011:16:36:42 +0800] [www.a.com/sid#7b0688][rid#9e8030/initial] (3) [perdir D:/Tomcat/webapps/ROOT/] strip per-dir prefix: D:/Tomcat/webapps/ROOT/a.html -> a.html
127.0.0.1 - - [08/Jun/2011:16:36:42 +0800] [www.a.com/sid#7b0688][rid#9e8030/initial] (3) [perdir D:/Tomcat/webapps/ROOT/] applying pattern '.*' to uri 'a.html'
127.0.0.1 - - [08/Jun/2011:16:36:42 +0800] [www.a.com/sid#7b0688][rid#9e8030/initial] (4) [perdir D:/Tomcat/webapps/ROOT/] RewriteCond: input='/a.html' pattern='^/a.html$' => matched
127.0.0.1 - - [08/Jun/2011:16:36:42 +0800] [www.a.com/sid#7b0688][rid#9e8030/initial] (4) [perdir D:/Tomcat/webapps/ROOT/] RewriteCond: input='D:/Tomcat/webapps/ROOT/a.html' pattern='!-s' => not-matched
127.0.0.1 - - [08/Jun/2011:16:36:42 +0800] [www.a.com/sid#7b0688][rid#9e8030/initial] (1) [perdir D:/Tomcat/webapps/ROOT/] pass through D:/Tomcat/webapps/ROOT/a.html

观察两个日志文件
不同地方是:
1:在.htaccess里面判断的条件有前缀如:[perdir D:/Tomcat/webapps/ROOT/]
2: RewriteCond: input='D:/Tomcat/webapps/ROOT/a.html' pattern='!-s' => not-matched
input是个绝对路径
而在http.conf里面的是个相对路径
如: RewriteCond: input='/a.html' pattern='!-s' => matched

呵呵,好像有点眉目了。是不是在http.conf里面的是相对路径所以判断是否存在的时候都是匹配呢。
修改http.conf文件为:
    RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME}   !-s
    RewriteRule ^/a\.html$  /b.html   [L]
及加上文档的根目录
测试成功。!!!!
日志如下:
127.0.0.1 - - [08/Jun/2011:16:53:39 +0800] [www.a.com/sid#7b0770][rid#9f8058/initial] (2) init rewrite engine with requested uri /a.html
127.0.0.1 - - [08/Jun/2011:16:53:39 +0800] [www.a.com/sid#7b0770][rid#9f8058/initial] (3) applying pattern '^/a\.html$' to uri '/a.html'
127.0.0.1 - - [08/Jun/2011:16:53:39 +0800] [www.a.com/sid#7b0770][rid#9f8058/initial] (4) RewriteCond: input='D:/Tomcat/webapps/ROOT/a.html' pattern='!-s' => not-matched
127.0.0.1 - - [08/Jun/2011:16:53:39 +0800] [www.a.com/sid#7b0770][rid#9f8058/initial] (1) pass through /a.html

RewriteCond 的 input 内容为文件的绝对路径了。
呵呵。
看了在http.conf文件里面和在.htaccess文件里面使用
RewriteCond %{REQUEST_FILE} !-f
是有区别的。
在.htaccess文件里面不用加文档的根路径及%{DOCUMENT_ROOT}因为.htaccess文件时目录级别的。apache会自动加上路径
而在http.conf文件里面的 apache不会自动加上文档根路径。
这是我根据apacher 的 Rewritelog文件做出的判断,不知道理解的是否正确,如果不对,请大侠们留言交流。
posted @ 2011-12-10 08:08  scgw  阅读(6504)  评论(0编辑  收藏  举报