在使用apachec重定向模块时,有这么一个语法:RewriteCond %{REQUEST_FILENAME} !-s,涉及到路径是用绝对路径还是相对路径的问题。 RewriteCond 究竟是判断相对路径还是绝对路径呢?阿里云北京分销商为您分享答案。
假如,用户访问网站的index.html页面,用apache判断index.html文件是否存在,如果不存在的话,转向index.php这个路径,
访问index.php路径是程序自动生成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
然后测试第一种情况
日志如下:
localhost – – [2019-06-20 :16:53:07 +0800] [www.a.com/sid#7b0770][rid#9e8030/initial] (2) init rewrite engine with requested uri /a.html
localhost – – [2019-06-20 :16:53:07 +0800] [www.a.com/sid#7b0770][rid#9e8030/initial] (3) applying pattern ‘^/a\.html$’ to uri ‘/a.html’
localhost – – [2019-06-20 :16:53:07 +0800] [www.a.com/sid#7b0770][rid#9e8030/initial] (4) RewriteCond: input=’/a.html’ pattern=’!-s’ => matched
localhost – – [2019-06-20 :16:53:07 +0800] [www.a.com/sid#7b0770][rid#9e8030/initial] (2) rewrite ‘/a.html’ -> ‘/b.html’
然后测试第二种情况
日志如下:
localhost – – [2019-06-20 :16:36:42 +0800] [www.a.com/sid#7b0688][rid#9e8030/initial] (2) init rewrite engine with requested uri /a.html
localhost – – [2019-06-20 :16:36:42 +0800] [www.a.com/sid#7b0688][rid#9e8030/initial] (1) pass through /a.html
localhost – – [2019-06-20 :16:36:42 +0800] [www.a.com/sid#7b0688][rid#9e8030/initial] (3) [perdir /home/web/ROOT/] strip per-dir prefix: /home/web/ROOT/a.html -> a.html
localhost – – [2019-06-20 :16:36:42 +0800] [www.a.com/sid#7b0688][rid#9e8030/initial] (3) [perdir /home/web/ROOT/] applying pattern ‘.*’ to uri ‘a.html’
localhost – – [2019-06-20 :16:36:42 +0800] [www.a.com/sid#7b0688][rid#9e8030/initial] (4) [perdir /home/web/ROOT/] RewriteCond: input=’/a.html’ pattern=’^/a.html$’ => matched
localhost – – [2019-06-20 :16:36:42 +0800] [www.a.com/sid#7b0688][rid#9e8030/initial] (4) [perdir /home/web/ROOT/] RewriteCond: input=’/home/web/ROOT/a.html’ pattern=’!-s’ => not-matched
localhost – – [2019-06-20 :16:36:42 +0800] [www.a.com/sid#7b0688][rid#9e8030/initial] (1) [perdir /home/web/ROOT/] pass through /home/web/ROOT/a.html
观察两个日志文件
不同地方是:
1:在.htaccess里面判断的条件有前缀如:[perdir /home/web/ROOT/]
2: RewriteCond: input=’/home/web/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]
及加上文档的根目录
测试成功。!!!!
日志如下:
localhost – – [2019-06-20 :16:53:39 +0800] [www.a.com/sid#7b0770][rid#9f8058/initial] (2) init rewrite engine with requested uri /a.html
localhost – – [2019-06-20 :16:53:39 +0800] [www.a.com/sid#7b0770][rid#9f8058/initial] (3) applying pattern ‘^/a\.html$’ to uri ‘/a.html’
localhost – – [2019-06-20 :16:53:39 +0800] [www.a.com/sid#7b0770][rid#9f8058/initial] (4) RewriteCond: input=’/home/web/ROOT/a.html’ pattern=’!-s’ => not-matched
localhost – – [2019-06-20 :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不会自动加上文档根路径。