Forum Moderators: phranque

Message Too Old, No Replies

Redirect User to his directory and keep the change internal

         

binarynot

3:59 pm on Jan 16, 2010 (gmt 0)



I'm trying to redirect urls like this:
example.org/stats shall redirect the User to his home (but without change to the url the browser sees). So the user still sees example.org/stats but is internal at example.org/stats/USER
The same shall happen to any url which starts with /stats
The Code I'm using is:

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]

This is only working as supposed if I use example.org/stats if I enter example.org/stats/something it redirects the user to example.org/stats/USER/something.
And it's not working as soon as the URL is of that kind: example.org/stats/something/page.html that's mostly caused by the first Rule missing an /, but when I add that I get an recursion which doesn't stops...somehow he adds all the time the user to the URL...

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

jdMorgan

4:52 pm on Jan 16, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Several problems... The worst problem (theoretically) is that you cannot compare a variable to a variable in a RewriteCond, as you tried to do in your third RewriteCond above.

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]

Jim

binarynot

5:09 pm on Jan 16, 2010 (gmt 0)



Thanks for the quick reply,
I'm quite new to mod_rewrite, so I'm not to confirm about what's the fastest solution.
The version you supplied is just an optimized one of mine? Or did I do something wrong as it still doesn't redirect correctly and doesn't work for subdirectories.

jdMorgan

6:00 pm on Jan 16, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It is a corrected and optimized version of what you posted.

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]

I should note that *some* prefix (such as "web" in the case discussed here) is required on the Remote_User subdirectories, because this prefix is the major mechanism of the loop prevention. However, this prefix is not required on the Remote_User's username itself, it's only required on the subdirectory-path.

Jim