Apache 2.4 Rewrite rule for root / matches but is

2019-08-10 19:30发布

问题:

EDIT: I reported this to Apache and it is indeed a bug, fixed in 2.4.9, see here ... For Ubuntu, there is an updated PPA here. For those who can't upgrade or don't want to compile, I posted a workaround below

This is already driving me nuts. Apache 2.4.7 on Ubuntu 14.04. The rewrite rule is simple:

RewriteCond %{REQUEST_URI} ^/?$
RewriteRule ^ /web/wp-content/cache/page_enhanced/HOSTNAME/_index.html [L]

Basically, I want to serve an .html file for /. The .html file exists and is served fine if requested directly. As you can see in the debug logs below (LogLevel alert rewrite:trace6 in the server conf), the cond on / does match (initial), but then Apache proceeds (subreq) and fails to match /index.html, /index.cgi, /index.pl and /index.php, and finally serves /index.php.

This is not expected. [L] should prevent Apache from continuing once a match is found.

I can confirm that this problem does not exist in Apache 2.2. This looks like a bug in Apache 2.4 ... or am I missing something?

Also, another frustrating thing is that this appears to be a problem only for /. The same rule but for a subdirectory (i.e. RewriteCond %{REQUEST_URI} ^/subdir/?$) works as expected in 2.4 too.

Rewrite debug logs:

# grep rewrite: /var/log/apache2/error.log
[Sat Apr 19 20:40:14.581933 2014] [rewrite:trace3] [pid 20459] mod_rewrite.c(468): [client 192.168.198.1:14715] 192.168.198.1 - - [HOSTNAME/sid#7fd7ed4f0528][rid#7fd7dabc90a0/initial] [perdir /var/www/HOSTNAME/] strip per-dir prefix: /var/www/HOSTNAME/ -> 
[Sat Apr 19 20:40:14.581957 2014] [rewrite:trace3] [pid 20459] mod_rewrite.c(468): [client 192.168.198.1:14715] 192.168.198.1 - - [HOSTNAME/sid#7fd7ed4f0528][rid#7fd7dabc90a0/initial] [perdir /var/www/HOSTNAME/] applying pattern '.*' to uri ''
[Sat Apr 19 20:40:14.581963 2014] [rewrite:trace4] [pid 20459] mod_rewrite.c(468): [client 192.168.198.1:14715] 192.168.198.1 - - [HOSTNAME/sid#7fd7ed4f0528][rid#7fd7dabc90a0/initial] [perdir /var/www/HOSTNAME/] RewriteCond: input='/' pattern='^/?$' => matched
[Sat Apr 19 20:40:14.581966 2014] [rewrite:trace2] [pid 20459] mod_rewrite.c(468): [client 192.168.198.1:14715] 192.168.198.1 - - [HOSTNAME/sid#7fd7ed4f0528][rid#7fd7dabc90a0/initial] [perdir /var/www/HOSTNAME/] rewrite '' -> '/web/wp-content/cache/page_enhanced/HOSTNAME/_index.html'
[Sat Apr 19 20:40:14.581969 2014] [rewrite:trace1] [pid 20459] mod_rewrite.c(468): [client 192.168.198.1:14715] 192.168.198.1 - - [HOSTNAME/sid#7fd7ed4f0528][rid#7fd7dabc90a0/initial] [perdir /var/www/HOSTNAME/] internal redirect with /web/wp-content/cache/page_enhanced/HOSTNAME/_index.html [INTERNAL REDIRECT]
[Sat Apr 19 20:40:14.582407 2014] [rewrite:trace3] [pid 20459] mod_rewrite.c(468): [client 192.168.198.1:14715] 192.168.198.1 - - [HOSTNAME/sid#7fd7ed4f0528][rid#7fd7dabcd0a0/subreq] [perdir /var/www/HOSTNAME/] strip per-dir prefix: /var/www/HOSTNAME/index.html -> index.html
[Sat Apr 19 20:40:14.582414 2014] [rewrite:trace3] [pid 20459] mod_rewrite.c(468): [client 192.168.198.1:14715] 192.168.198.1 - - [HOSTNAME/sid#7fd7ed4f0528][rid#7fd7dabcd0a0/subreq] [perdir /var/www/HOSTNAME/] applying pattern '.*' to uri 'index.html'
[Sat Apr 19 20:40:14.582418 2014] [rewrite:trace4] [pid 20459] mod_rewrite.c(468): [client 192.168.198.1:14715] 192.168.198.1 - - [HOSTNAME/sid#7fd7ed4f0528][rid#7fd7dabcd0a0/subreq] [perdir /var/www/HOSTNAME/] RewriteCond: input='/index.html' pattern='^/?$' => not-matched
[Sat Apr 19 20:40:14.582420 2014] [rewrite:trace1] [pid 20459] mod_rewrite.c(468): [client 192.168.198.1:14715] 192.168.198.1 - - [HOSTNAME/sid#7fd7ed4f0528][rid#7fd7dabcd0a0/subreq] [perdir /var/www/HOSTNAME/] pass through /var/www/HOSTNAME/index.html
[Sat Apr 19 20:40:14.582767 2014] [rewrite:trace3] [pid 20459] mod_rewrite.c(468): [client 192.168.198.1:14715] 192.168.198.1 - - [HOSTNAME/sid#7fd7ed4f0528][rid#7fd7dabcd0a0/subreq] [perdir /var/www/HOSTNAME/] strip per-dir prefix: /var/www/HOSTNAME/index.cgi -> index.cgi
[Sat Apr 19 20:40:14.582772 2014] [rewrite:trace3] [pid 20459] mod_rewrite.c(468): [client 192.168.198.1:14715] 192.168.198.1 - - [HOSTNAME/sid#7fd7ed4f0528][rid#7fd7dabcd0a0/subreq] [perdir /var/www/HOSTNAME/] applying pattern '.*' to uri 'index.cgi'
[Sat Apr 19 20:40:14.582775 2014] [rewrite:trace4] [pid 20459] mod_rewrite.c(468): [client 192.168.198.1:14715] 192.168.198.1 - - [HOSTNAME/sid#7fd7ed4f0528][rid#7fd7dabcd0a0/subreq] [perdir /var/www/HOSTNAME/] RewriteCond: input='/index.cgi' pattern='^/?$' => not-matched
[Sat Apr 19 20:40:14.582777 2014] [rewrite:trace1] [pid 20459] mod_rewrite.c(468): [client 192.168.198.1:14715] 192.168.198.1 - - [HOSTNAME/sid#7fd7ed4f0528][rid#7fd7dabcd0a0/subreq] [perdir /var/www/HOSTNAME/] pass through /var/www/HOSTNAME/index.cgi
[Sat Apr 19 20:40:14.583131 2014] [rewrite:trace3] [pid 20459] mod_rewrite.c(468): [client 192.168.198.1:14715] 192.168.198.1 - - [HOSTNAME/sid#7fd7ed4f0528][rid#7fd7dabcd0a0/subreq] [perdir /var/www/HOSTNAME/] strip per-dir prefix: /var/www/HOSTNAME/index.pl -> index.pl
[Sat Apr 19 20:40:14.583136 2014] [rewrite:trace3] [pid 20459] mod_rewrite.c(468): [client 192.168.198.1:14715] 192.168.198.1 - - [HOSTNAME/sid#7fd7ed4f0528][rid#7fd7dabcd0a0/subreq] [perdir /var/www/HOSTNAME/] applying pattern '.*' to uri 'index.pl'
[Sat Apr 19 20:40:14.583143 2014] [rewrite:trace4] [pid 20459] mod_rewrite.c(468): [client 192.168.198.1:14715] 192.168.198.1 - - [HOSTNAME/sid#7fd7ed4f0528][rid#7fd7dabcd0a0/subreq] [perdir /var/www/HOSTNAME/] RewriteCond: input='/index.pl' pattern='^/?$' => not-matched
[Sat Apr 19 20:40:14.583146 2014] [rewrite:trace1] [pid 20459] mod_rewrite.c(468): [client 192.168.198.1:14715] 192.168.198.1 - - [HOSTNAME/sid#7fd7ed4f0528][rid#7fd7dabcd0a0/subreq] [perdir /var/www/HOSTNAME/] pass through /var/www/HOSTNAME/index.pl
[Sat Apr 19 20:40:14.583375 2014] [rewrite:trace3] [pid 20459] mod_rewrite.c(468): [client 192.168.198.1:14715] 192.168.198.1 - - [HOSTNAME/sid#7fd7ed4f0528][rid#7fd7dabcd0a0/subreq] [perdir /var/www/HOSTNAME/] strip per-dir prefix: /var/www/HOSTNAME/index.php -> index.php
[Sat Apr 19 20:40:14.583380 2014] [rewrite:trace3] [pid 20459] mod_rewrite.c(468): [client 192.168.198.1:14715] 192.168.198.1 - - [HOSTNAME/sid#7fd7ed4f0528][rid#7fd7dabcd0a0/subreq] [perdir /var/www/HOSTNAME/] applying pattern '.*' to uri 'index.php'
[Sat Apr 19 20:40:14.583383 2014] [rewrite:trace4] [pid 20459] mod_rewrite.c(468): [client 192.168.198.1:14715] 192.168.198.1 - - [HOSTNAME/sid#7fd7ed4f0528][rid#7fd7dabcd0a0/subreq] [perdir /var/www/HOSTNAME/] RewriteCond: input='/index.php' pattern='^/?$' => not-matched
[Sat Apr 19 20:40:14.583386 2014] [rewrite:trace1] [pid 20459] mod_rewrite.c(468): [client 192.168.198.1:14715] 192.168.198.1 - - [HOSTNAME/sid#7fd7ed4f0528][rid#7fd7dabcd0a0/subreq] [perdir /var/www/HOSTNAME/] pass through /var/www/HOSTNAME/index.php

Any clues?

回答1:

For now, my (ugly) workaround solution is:

RewriteCond %{REQUEST_URI} ^(/index.\w+|/)?$
RewriteRule ^ /web/wp-content/cache/page_enhanced/HOSTNAME/_index.html [L]

in other words, I'm matching against all possible /index.* as well as /. The former should not be necessary and in my case it complicates my setup as index.php should match other rules so I needed to work around that too...

Note that this bug (?) only happens for /. Same rule for subdirectories work fine, i.e.:

RewriteCond %{REQUEST_URI} ^/subdir/?$
RewriteRule ^ /web/wp-content/cache/page_enhanced/HOSTNAME/subdir/_index.html [L]

works just fine ...