mod_rewrite all but two files causing loop

Posted by mpounsett on Server Fault See other posts from Server Fault or by mpounsett
Published on 2014-05-29T14:55:03Z Indexed on 2014/05/29 21:35 UTC
Read the original article Hit count: 159

Filed under:
|

I'm trying to set up a web site to allow the creation of a semaphore file to close the site. The logic I want to follow is:

  1. when the semaphore file exists
  2. and the request is not for /style.css or /favicon.icon
  3. show the content of /closed.html

I have 1 and 3 working, but my exceptions for 2 result in a processing loop when style.css or favicon.ico are requested. This is my most recent attempt:

RewriteEngine on
RewriteCond %{REQUEST_URI} !^/style.css
RewriteCond %{REQUEST_URI} !^/favicon.ico
RewriteCond /usr/local/etc/site/closed -f
RewriteRule ^.*$ /closed.html [L]

This is in a VirtualHost block, not in a Directory. There is no .htaccess file in play.

I have also recently tried this, based on an answer I found elsewhere, but with the same (looping) result:

RewriteCond %{REQUEST_URI} ^/style.css [OR]
RewriteCond %{REQUEST_URI} ^/favicon.ico
RewriteRule ^.*$ - [L]
RewriteCond /usr/local/etc/site/closed -f
RewriteRule ^.*$ /closed.html [L]

I expect a request for /style.css or /favicon.ico to fail to match one of the first two rewrite conditions, which should prevent the URI from being rewritten, which should stop the mod_rewrite iteration. However, mod_rewrite seems to think the URI has been rewritten in those cases, and iterates over the rules again (and again, and again). The above works properly in all cases except for style.css or favicon.ico. In those cases I exceed the loop limits.

What am I missing here to cause the rewrite iteration to stop when someone requests style.css or favicon.ico?

EDIT: Here's a loglevel 9 example of what happens using the first ruleset when a request arrives for /style.css. This is just the first two iterations.. it continues to loop identically until the limit is reached.

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

© Server Fault or respective owner

Related posts about apache-2.2

Related posts about mod-rewrite