Forum Moderators: phranque
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/stats/[a-zA-Z0-9\.]*$
RewriteCond %{REMOTE_USER} ^web[0-9]+$
RewriteCond %{REQUEST_URI} !^/stats/%{REMOTE_USER}
RewriteRule (.*) /stats/%{REMOTE_USER}/$1 [L]
The rewrite.log looks that for example.org/stats/something:
31.-1.0.21 - USER [16/Jan/2010:16:51:37 +0100] [example.org/sid#80f178][rid#ac5d98/initial] (3) [perdir /var/www/stats/] strip per-dir prefix: /var/www/stats/something -> something
31.-1.0.21 - USER [16/Jan/2010:16:51:37 +0100] [example.org/sid#80f178][rid#ac5d98/initial] (3) [perdir /var/www/stats/] applying pattern '(.*)' to uri 'something'
31.-1.0.21 - USER [16/Jan/2010:16:51:37 +0100] [example.org/sid#80f178][rid#ac5d98/initial] (4) [perdir /var/www/stats/] RewriteCond: input='/stats/something' pattern='^/stats/[a-zA-Z0-9\.]*$' => matched
31.-1.0.21 - USER [16/Jan/2010:16:51:37 +0100] [example.org/sid#80f178][rid#ac5d98/initial] (4) [perdir /var/www/stats/] RewriteCond: input='USER' pattern='^web[0-9]+$' => matched
31.-1.0.21 - USER [16/Jan/2010:16:51:37 +0100] [example.org/sid#80f178][rid#ac5d98/initial] (4) [perdir /var/www/stats/] RewriteCond: input='/stats/something' pattern='!^/stats/%{REMOTE_USER}' => matched
31.-1.0.21 - USER [16/Jan/2010:16:51:37 +0100] [example.org/sid#80f178][rid#ac5d98/initial] (2) [perdir /var/www/stats/] rewrite 'something' -> '/stats/USER/something'
31.-1.0.21 - USER [16/Jan/2010:16:51:37 +0100] [example.org/sid#80f178][rid#ac5d98/initial] (1) [perdir /var/www/stats/] internal redirect with /stats/USER/something [INTERNAL REDIRECT]
31.-1.0.21 - USER [16/Jan/2010:16:51:37 +0100] [example.org/sid#80f178][rid#ab7fb0/initial/redir#1] (3) [perdir /var/www/stats/] strip per-dir prefix: /var/www/stats/USER/something -> USER/something
31.-1.0.21 - USER [16/Jan/2010:16:51:37 +0100] [example.org/sid#80f178][rid#ab7fb0/initial/redir#1] (3) [perdir /var/www/stats/] applying pattern '(.*)' to uri 'USER/something'
31.-1.0.21 - USER [16/Jan/2010:16:51:37 +0100] [example.org/sid#80f178][rid#ab7fb0/initial/redir#1] (4) [perdir /var/www/stats/] RewriteCond: input='/stats/USER/something' pattern='^/stats/[a-zA-Z0-9\.]*$' => not-matched
31.-1.0.21 - USER [16/Jan/2010:16:51:37 +0100] [example.org/sid#80f178][rid#ab7fb0/initial/redir#1] (1) [perdir /var/www/stats/] pass through /var/www/stats/USER/something
31.-1.0.21 - USER [16/Jan/2010:16:51:37 +0100] [example.org/sid#80f178][rid#a86738/initial] (3) [perdir /var/www/stats/] strip per-dir prefix: /var/www/stats/USER/something/ -> USER/something/
31.-1.0.21 - USER [16/Jan/2010:16:51:37 +0100] [example.org/sid#80f178][rid#a86738/initial] (3) [perdir /var/www/stats/] applying pattern '(.*)' to uri 'USER/something/'
31.-1.0.21 - USER [16/Jan/2010:16:51:37 +0100] [example.org/sid#80f178][rid#a86738/initial] (4) [perdir /var/www/stats/] RewriteCond: input='/stats/USER/something/' pattern='^/stats/[a-zA-Z0-9\.]*$' => not-matched
31.-1.0.21 - USER [16/Jan/2010:16:51:37 +0100] [example.org/sid#80f178][rid#a86738/initial] (1) [perdir /var/www/stats/] pass through /var/www/stats/USER/something/
31.-1.0.21 - USER [16/Jan/2010:16:51:37 +0100] [example.org/sid#80f178][rid#a3a868/subreq] (3) [perdir /var/www/stats/] strip per-dir prefix: /var/www/stats/USER/something/index.html -> USER/something/index.html
31.-1.0.21 - USER [16/Jan/2010:16:51:37 +0100] [example.org/sid#80f178][rid#a3a868/subreq] (3) [perdir /var/www/stats/] applying pattern '(.*)' to uri 'USER/something/index.html'
31.-1.0.21 - USER [16/Jan/2010:16:51:37 +0100] [example.org/sid#80f178][rid#a3a868/subreq] (4) [perdir /var/www/stats/] RewriteCond: input='/stats/USER/something/index.html' pattern='^/stats/[a-zA-Z0-9\.]*$' => not-matched
31.-1.0.21 - USER [16/Jan/2010:16:51:37 +0100] [example.org/sid#80f178][rid#a3a868/subreq] (1) [perdir /var/www/stats/] pass through /var/www/stats/USER/something/index.html
31.-1.0.21 - USER [16/Jan/2010:16:51:37 +0100] [example.org/sid#80f178][rid#aac138/subreq] (3) [perdir /var/www/stats/] strip per-dir prefix: /var/www/stats/USER/something/index.cgi -> USER/something/index.cgi
31.-1.0.21 - USER [16/Jan/2010:16:51:37 +0100] [example.org/sid#80f178][rid#aac138/subreq] (3) [perdir /var/www/stats/] applying pattern '(.*)' to uri 'USER/something/index.cgi'
31.-1.0.21 - USER [16/Jan/2010:16:51:37 +0100] [example.org/sid#80f178][rid#aac138/subreq] (4) [perdir /var/www/stats/] RewriteCond: input='/stats/USER/something/index.cgi' pattern='^/stats/[a-zA-Z0-9\.]*$' => not-matched
31.-1.0.21 - USER [16/Jan/2010:16:51:37 +0100] [example.org/sid#80f178][rid#aac138/subreq] (1) [perdir /var/www/stats/] pass through /var/www/stats/USER/something/index.cgi
31.-1.0.21 - USER [16/Jan/2010:16:51:37 +0100] [example.org/sid#80f178][rid#a3a868/subreq] (3) [perdir /var/www/stats/] strip per-dir prefix: /var/www/stats/USER/something/index.pl -> USER/something/index.pl
31.-1.0.21 - USER [16/Jan/2010:16:51:37 +0100] [example.org/sid#80f178][rid#a3a868/subreq] (3) [perdir /var/www/stats/] applying pattern '(.*)' to uri 'USER/something/index.pl'
31.-1.0.21 - USER [16/Jan/2010:16:51:37 +0100] [example.org/sid#80f178][rid#a3a868/subreq] (4) [perdir /var/www/stats/] RewriteCond: input='/stats/USER/something/index.pl' pattern='^/stats/[a-zA-Z0-9\.]*$' => not-matched
31.-1.0.21 - USER [16/Jan/2010:16:51:37 +0100] [example.org/sid#80f178][rid#a3a868/subreq] (1) [perdir /var/www/stats/] pass through /var/www/stats/USER/something/index.pl
31.-1.0.21 - USER [16/Jan/2010:16:51:37 +0100] [example.org/sid#80f178][rid#a88748/subreq] (3) [perdir /var/www/stats/] strip per-dir prefix: /var/www/stats/USER/something/index.php -> USER/something/index.php
31.-1.0.21 - USER [16/Jan/2010:16:51:37 +0100] [example.org/sid#80f178][rid#a88748/subreq] (3) [perdir /var/www/stats/] applying pattern '(.*)' to uri 'USER/something/index.php'
31.-1.0.21 - USER [16/Jan/2010:16:51:37 +0100] [example.org/sid#80f178][rid#a88748/subreq] (4) [perdir /var/www/stats/] RewriteCond: input='/stats/USER/something/index.php' pattern='^/stats/[a-zA-Z0-9\.]*$' => not-matched
31.-1.0.21 - USER [16/Jan/2010:16:51:37 +0100] [example.org/sid#80f178][rid#a88748/subreq] (1) [perdir /var/www/stats/] pass through /var/www/stats/USER/something/index.php
The practical problems are the recursion and code inefficiency. I'd suggest:
RewriteEngine on
#
RewriteCond $1 !^stats/web[0-9]+/
RewriteCond %{REMOTE_USER} ^(web[0-9]+)$
RewriteRule ^(stats/[a-zA-Z0-9.]*)$ stats/%2/$1 [L]
It prevents recursion based on the presence of "/web123/" or similar in the current path, which we can presume was added there by the rule itself. Lacking this test, the rule will be recursive. You had attempted to stop the recursion by testing for the presence of %{REMOTE_USER} in the current path, but this test falled for two reasons: First, you cannot put a variable on the "right side" of a RewriteCond, and second, the prefix "web" was missing, and so the test would have failed even if RewriteCond *could* de-reference a variable in it's pattern.
It is optimized in that the URL-path test is moved to the RewriteRule pattern. The RewriteConds won't even be evaluated if the RewriteRule pattern doesn't match.
To rewrite subdirectories and filenames, i.e. to rewrite
URL example.com/stats/foo/bar/page.html --> filepath /stats/web123/foo/bar/page.html
with %{REMOTE_USER} names such as "web123" :
RewriteEngine on
#
RewriteCond $1 !^stats/web[0-9]+/
RewriteCond %{REMOTE_USER} ^(web[0-9]+)$
RewriteRule ^(stats/[a-zA-Z0-9.]*(/[^/]+)*)$ stats/%2/$1 [L]
Jim