我试图build立一个网站,以允许创build一个信号量文件closures该网站。 我想遵循的逻辑是:
我有1和3工作,但是我的例外2导致处理循环时style.css或favicon.ico请求。 这是我最近的尝试:
RewriteEngine on RewriteCond %{REQUEST_URI} !^/style.css RewriteCond %{REQUEST_URI} !^/favicon.ico RewriteCond /usr/local/etc/site/closed -f RewriteRule ^.*$ /closed.html [L]
这是在一个VirtualHost块中,而不是在一个目录中。 没有.htaccess文件在播放。
我最近也尝试过,基于我在其他地方find的答案,但结果是一样的(循环):
RewriteCond %{REQUEST_URI} ^/style.css [OR] RewriteCond %{REQUEST_URI} ^/favicon.ico RewriteRule ^.*$ - [L] RewriteCond /usr/local/etc/site/closed -f RewriteRule ^.*$ /closed.html [L]
我希望/style.css或/favicon.ico的请求无法匹配前两个重写条件之一,这会阻止URI被重写,这应该会停止mod_rewrite迭代。 然而,mod_rewrite似乎认为在这些情况下URI已被重写,并且重复遍历规则(以及再次和重复)。 除了style.css或favicon.ico以外,上述方法在所有情况下均正常工作。 在这些情况下,我超过了循环限制。
当有人要求style.css或favicon.ico时,我在这里丢失什么导致重写迭代停止?
编辑 :这是一个loglevel 9例子,当请求到达/style.css时,使用第一个规则集会发生什么。 这只是前两个迭代..它继续相同的循环,直到达到极限。
2001:4900:1044:0:145f:826e:6436:dc1 - - [29/May/2014:15:29:26 +0000] [host.example/sid#80c1c48b0][rid#80c1db0a0/initial] (2) init rewrite engine with requested uri /style.css 2001:4900:1044:0:145f:826e:6436:dc1 - - [29/May/2014:15:29:26 +0000] [host.example/sid#80c1c48b0][rid#80c1db0a0/initial] (3) applying pattern '^.*$' to uri '/style.css' 2001:4900:1044:0:145f:826e:6436:dc1 - - [29/May/2014:15:29:26 +0000] [host.example/sid#80c1c48b0][rid#80c1db0a0/initial] (4) RewriteCond: input='/style.css' pattern='!^/style.css' => not-matched 2001:4900:1044:0:145f:826e:6436:dc1 - - [29/May/2014:15:29:26 +0000] [host.example/sid#80c1c48b0][rid#80c1db0a0/initial] (1) pass through /style.css 2001:4900:1044:0:145f:826e:6436:dc1 - - [29/May/2014:15:29:26 +0000] [host.example/sid#80c1c48b0][rid#80c1dd0a0/initial] (2) init rewrite engine with requested uri /style.css 2001:4900:1044:0:145f:826e:6436:dc1 - - [29/May/2014:15:29:26 +0000] [host.example/sid#80c1c48b0][rid#80c1dd0a0/initial] (3) applying pattern '^.*$' to uri '/style.css' 2001:4900:1044:0:145f:826e:6436:dc1 - - [29/May/2014:15:29:26 +0000] [host.example/sid#80c1c48b0][rid#80c1dd0a0/initial] (4) RewriteCond: input='/style.css' pattern='!^/style.css' => not-matched 2001:4900:1044:0:145f:826e:6436:dc1 - - [29/May/2014:15:29:26 +0000] [host.example/sid#80c1c48b0][rid#80c1dd0a0/initial] (1) pass through /style.css
尝试添加
RewriteLog /tmp/rewrite.log RewriteLogLevel 4
在日志文件中查看是否有帮助。
/closed.html是无限循环的原因。
我认为这可能会解决你的问题。 我刚刚在你的第一次尝试中加了一行(见第4行):
RewriteEngine on RewriteCond %{REQUEST_URI} !^/style.css RewriteCond %{REQUEST_URI} !^/favicon.ico RewriteCond %{REQUEST_URI} !^/closed.html RewriteCond /usr/local/etc/site/closed -f RewriteRule ^.*$ /closed.html [L]
我在一个最新的CentOS 6的Apache设置中尝试了你的设置。
这是我的configuration
RewriteEngine on RewriteCond %{REQUEST_URI} !^/style.css RewriteCond %{REQUEST_URI} !^/favicon.ico RewriteCond /tmp/closed -F RewriteRule ^.*$ /closed.html [L]
以下是一个certificate它工作的日志:
$ curl https://localhost/ <!DOCTYPE html> <title>It works!</title> <p>It works!</p> $ curl https://localhost/style.css html { background: black; color: white; } $ touch /tmp/closed $ curl https://localhost/ <!DOCTYPE html> <title>We're closed</title> <p>We're closed</p> $ curl https://localhost/style.css html { background: black; color: white; }
编辑这不是你想要的问题,我把它留在这里,所以它可以帮助其他类似的问题。
解决你的问题的另一种方法是这样的
<LocationMatch "^/(?!410\.html)"> RedirectMatch gone .* </LocationMatch> ErrorDocument 410 /410.html
410.html是你的错误页面。 这将发送410 Gone状态代码到客户端,表明该站点已经消失,不会回来。