Apache mod_rewrite redirecting to base URL - apache

I'm busy migrating a production codeigniter project to a new server. The current setup has been working fine for almost a year now but while moving to the new LAMP stack I'm running into a weird problem with rewriting the index.php back into the URL.
This is the mod_rewrite is as follows
RewriteEngine On
RewriteBase /bookings/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
Like I said this has been working fine in the current setup however on the new server links from the base page always are rewritten to the same page. Both servers run Debian 7.5 and Apache 2.2.22
I turned rewrite logging on for both servers and the new server is doing an extra rewrite step that I cannot explain.
Current server:
105.237.x.x - - [16/May/2014:09:45:11 +0200] [www.domain.tld/sid#7fd5c46fbfc8][rid#7fd5c45350a0/initial] (3) [perdir /var/www/vhosts/domain.tld/html/bookings/] add path info postfix: /var/www/vhosts/domain.tld/html/bookings/school -> /var/www/vhosts/domain.tld/html/bookings/school/event/1
105.237.x.x - - [16/May/2014:09:45:11 +0200] [www.domain.tld/sid#7fd5c46fbfc8][rid#7fd5c45350a0/initial] (3) [perdir /var/www/vhosts/domain.tld/html/bookings/] strip per-dir prefix: /var/www/vhosts/domain.tld/html/bookings/school/event/1 -> school/event/1
105.237.x.x - - [16/May/2014:09:45:11 +0200] [www.domain.tld/sid#7fd5c46fbfc8][rid#7fd5c45350a0/initial] (3) [perdir /var/www/vhosts/domain.tld/html/bookings/] applying pattern '^(.*)$' to uri 'school/event/1'
105.237.x.x - - [16/May/2014:09:45:11 +0200] [www.domain.tld/sid#7fd5c46fbfc8][rid#7fd5c45350a0/initial] (4) [perdir /var/www/vhosts/domain.tld/html/bookings/] RewriteCond: input='/var/www/vhosts/domain.tld/html/bookings/school' pattern='!-f' => matched
105.237.x.x - - [16/May/2014:09:45:11 +0200] [www.domain.tld/sid#7fd5c46fbfc8][rid#7fd5c45350a0/initial] (4) [perdir /var/www/vhosts/domain.tld/html/bookings/] RewriteCond: input='/var/www/vhosts/domain.tld/html/bookings/school' pattern='!-d' => matched
105.237.x.x - - [16/May/2014:09:45:11 +0200] [www.domain.tld/sid#7fd5c46fbfc8][rid#7fd5c45350a0/initial] (2) [perdir /var/www/vhosts/domain.tld/html/bookings/] rewrite 'school/event/1' -> 'index.php/school/event/1'
105.237.x.x - - [16/May/2014:09:45:11 +0200] [www.domain.tld/sid#7fd5c46fbfc8][rid#7fd5c45350a0/initial] (3) [perdir /var/www/vhosts/domain.tld/html/bookings/] add per-dir prefix: index.php/school/event/1 -> /var/www/vhosts/domain.tld/html/bookings/index.php/school/event/1
105.237.x.x - - [16/May/2014:09:45:11 +0200] [www.domain.tld/sid#7fd5c46fbfc8][rid#7fd5c45350a0/initial] (2) [perdir /var/www/vhosts/domain.tld/html/bookings/] trying to replace prefix /var/www/vhosts/domain.tld/html/bookings/ with /bookings/
105.237.x.x - - [16/May/2014:09:45:11 +0200] [www.domain.tld/sid#7fd5c46fbfc8][rid#7fd5c45350a0/initial] (5) strip matching prefix: /var/www/vhosts/domain.tld/html/bookings/index.php/school/event/1 -> index.php/school/event/1
105.237.x.x - - [16/May/2014:09:45:11 +0200] [www.domain.tld/sid#7fd5c46fbfc8][rid#7fd5c45350a0/initial] (4) add subst prefix: index.php/school/event/1 -> /bookings/index.php/school/event/1
105.237.x.x - - [16/May/2014:09:45:11 +0200] [www.domain.tld/sid#7fd5c46fbfc8][rid#7fd5c45350a0/initial] (1) [perdir /var/www/vhosts/domain.tld/html/bookings/] internal redirect with /bookings/index.php/school/event/1 [INTERNAL REDIRECT]
105.237.x.x - - [16/May/2014:09:45:11 +0200] [www.domain.tld/sid#7fd5c46fbfc8][rid#7fd5b3e057c8/initial/redir#1] (3) [perdir /var/www/vhosts/domain.tld/html/bookings/] add path info postfix: /var/www/vhosts/domain.tld/html/bookings/index.php -> /var/www/vhosts/domain.tld/html/bookings/index.php/school/event/1
105.237.x.x - - [16/May/2014:09:45:11 +0200] [www.domain.tld/sid#7fd5c46fbfc8][rid#7fd5b3e057c8/initial/redir#1] (3) [perdir /var/www/vhosts/domain.tld/html/bookings/] strip per-dir prefix: /var/www/vhosts/domain.tld/html/bookings/index.php/school/event/1 -> index.php/school/event/1
105.237.x.x - - [16/May/2014:09:45:11 +0200] [www.domain.tld/sid#7fd5c46fbfc8][rid#7fd5b3e057c8/initial/redir#1] (3) [perdir /var/www/vhosts/domain.tld/html/bookings/] applying pattern '^(.*)$' to uri 'index.php/school/event/1'
105.237.x.x - - [16/May/2014:09:45:11 +0200] [www.domain.tld/sid#7fd5c46fbfc8][rid#7fd5b3e057c8/initial/redir#1] (4) [perdir /var/www/vhosts/domain.tld/html/bookings/] RewriteCond: input='/var/www/vhosts/domain.tld/html/bookings/index.php' pattern='!-f' => not-matched
105.237.x.x - - [16/May/2014:09:45:11 +0200] [www.domain.tld/sid#7fd5c46fbfc8][rid#7fd5b3e057c8/initial/redir#1] (1) [perdir /var/www/vhosts/domain.tld/html/bookings/] pass through /var/www/vhosts/domain.tld/html/bookings/index.php
New server:
### First section is identical ###
105.237.x.x - - [16/May/2014:10:11:18 +0200] [www.domain.tld/sid#7f33674c04f0][rid#7f33673240a0/initial] (3) [perdir /var/www/vhosts/domain.tld/html/bookings/] add path info postfix: /var/www/vhosts/domain.tld/html/bookings/school -> /var/www/vhosts/domain.tld/html/bookings/school/event/1
105.237.x.x - - [16/May/2014:10:11:18 +0200] [www.domain.tld/sid#7f33674c04f0][rid#7f33673240a0/initial] (3) [perdir /var/www/vhosts/domain.tld/html/bookings/] strip per-dir prefix: /var/www/vhosts/domain.tld/html/bookings/school/event/1 -> school/event/1
105.237.x.x - - [16/May/2014:10:11:18 +0200] [www.domain.tld/sid#7f33674c04f0][rid#7f33673240a0/initial] (3) [perdir /var/www/vhosts/domain.tld/html/bookings/] applying pattern '^(.*)$' to uri 'school/event/1'
105.237.x.x - - [16/May/2014:10:11:18 +0200] [www.domain.tld/sid#7f33674c04f0][rid#7f33673240a0/initial] (4) [perdir /var/www/vhosts/domain.tld/html/bookings/] RewriteCond: input='/var/www/vhosts/domain.tld/html/bookings/school' pattern='!-f' => matched
105.237.x.x - - [16/May/2014:10:11:18 +0200] [www.domain.tld/sid#7f33674c04f0][rid#7f33673240a0/initial] (4) [perdir /var/www/vhosts/domain.tld/html/bookings/] RewriteCond: input='/var/www/vhosts/domain.tld/html/bookings/school' pattern='!-d' => matched
105.237.x.x - - [16/May/2014:10:11:18 +0200] [www.domain.tld/sid#7f33674c04f0][rid#7f33673240a0/initial] (2) [perdir /var/www/vhosts/domain.tld/html/bookings/] rewrite 'school/event/1' -> 'index.php/school/event/1'
105.237.x.x - - [16/May/2014:10:11:18 +0200] [www.domain.tld/sid#7f33674c04f0][rid#7f33673240a0/initial] (3) [perdir /var/www/vhosts/domain.tld/html/bookings/] add per-dir prefix: index.php/school/event/1 -> /var/www/vhosts/domain.tld/html/bookings/index.php/school/event/1
105.237.x.x - - [16/May/2014:10:11:18 +0200] [www.domain.tld/sid#7f33674c04f0][rid#7f33673240a0/initial] (2) [perdir /var/www/vhosts/domain.tld/html/bookings/] trying to replace prefix /var/www/vhosts/domain.tld/html/bookings/ with /bookings/
105.237.x.x - - [16/May/2014:10:11:18 +0200] [www.domain.tld/sid#7f33674c04f0][rid#7f33673240a0/initial] (5) strip matching prefix: /var/www/vhosts/domain.tld/html/bookings/index.php/school/event/1 -> index.php/school/event/1
105.237.x.x - - [16/May/2014:10:11:18 +0200] [www.domain.tld/sid#7f33674c04f0][rid#7f33673240a0/initial] (4) add subst prefix: index.php/school/event/1 -> /bookings/index.php/school/event/1
105.237.x.x - - [16/May/2014:10:11:18 +0200] [www.domain.tld/sid#7f33674c04f0][rid#7f33673240a0/initial] (1) [perdir /var/www/vhosts/domain.tld/html/bookings/] internal redirect with /bookings/index.php/school/event/1 [INTERNAL REDIRECT]
105.237.x.x - - [16/May/2014:10:11:18 +0200] [www.domain.tld/sid#7f33674c04f0][rid#7f3367321b58/initial/redir#1] (3) [perdir /var/www/vhosts/domain.tld/html/bookings/] add path info postfix: /var/www/vhosts/domain.tld/html/bookings/index.php -> /var/www/vhosts/domain.tld/html/bookings/index.php/school/event/1
105.237.x.x - - [16/May/2014:10:11:18 +0200] [www.domain.tld/sid#7f33674c04f0][rid#7f3367321b58/initial/redir#1] (3) [perdir /var/www/vhosts/domain.tld/html/bookings/] strip per-dir prefix: /var/www/vhosts/domain.tld/html/bookings/index.php/school/event/1 -> index.php/school/event/1
105.237.x.x - - [16/May/2014:10:11:18 +0200] [www.domain.tld/sid#7f33674c04f0][rid#7f3367321b58/initial/redir#1] (3) [perdir /var/www/vhosts/domain.tld/html/bookings/] applying pattern '^(.*)$' to uri 'index.php/school/event/1'
105.237.x.x - - [16/May/2014:10:11:18 +0200] [www.domain.tld/sid#7f33674c04f0][rid#7f3367321b58/initial/redir#1] (4) [perdir /var/www/vhosts/domain.tld/html/bookings/] RewriteCond: input='/var/www/vhosts/domain.tld/html/bookings/index.php' pattern='!-f' => not-matched
105.237.x.x - - [16/May/2014:10:11:18 +0200] [www.domain.tld/sid#7f33674c04f0][rid#7f3367321b58/initial/redir#1] (1) [perdir /var/www/vhosts/domain.tld/html/bookings/] pass through /var/www/vhosts/domain.tld/html/bookings/index.php
### The extra redirect ###
105.237.x.x - - [16/May/2014:10:11:18 +0200] [www.domain.tld/sid#7f33674c04f0][rid#7f33673220a0/initial] (3) [perdir /var/www/vhosts/domain.tld/html/bookings/] strip per-dir prefix: /var/www/vhosts/domain.tld/html/bookings/ ->
105.237.x.x - - [16/May/2014:10:11:18 +0200] [www.domain.tld/sid#7f33674c04f0][rid#7f33673220a0/initial] (3) [perdir /var/www/vhosts/domain.tld/html/bookings/] applying pattern '^(.*)$' to uri ''
105.237.x.x - - [16/May/2014:10:11:18 +0200] [www.domain.tld/sid#7f33674c04f0][rid#7f33673220a0/initial] (4) [perdir /var/www/vhosts/domain.tld/html/bookings/] RewriteCond: input='/var/www/vhosts/domain.tld/html/bookings/' pattern='!-f' => matched
105.237.x.x - - [16/May/2014:10:11:18 +0200] [www.domain.tld/sid#7f33674c04f0][rid#7f33673220a0/initial] (4) [perdir /var/www/vhosts/domain.tld/html/bookings/] RewriteCond: input='/var/www/vhosts/domain.tld/html/bookings/' pattern='!-d' => not-matched
105.237.x.x - - [16/May/2014:10:11:18 +0200] [www.domain.tld/sid#7f33674c04f0][rid#7f33673220a0/initial] (1) [perdir /var/www/vhosts/domain.tld/html/bookings/] pass through /var/www/vhosts/domain.tld/html/bookings/
105.237.x.x - - [16/May/2014:10:11:18 +0200] [www.domain.tld/sid#7f33674c04f0][rid#7f336731c0a0/subreq] (3) [perdir /var/www/vhosts/domain.tld/html/bookings/] strip per-dir prefix: /var/www/vhosts/domain.tld/html/bookings/index.php -> index.php
105.237.x.x - - [16/May/2014:10:11:18 +0200] [www.domain.tld/sid#7f33674c04f0][rid#7f336731c0a0/subreq] (3) [perdir /var/www/vhosts/domain.tld/html/bookings/] applying pattern '^(.*)$' to uri 'index.php'
105.237.x.x - - [16/May/2014:10:11:18 +0200] [www.domain.tld/sid#7f33674c04f0][rid#7f336731c0a0/subreq] (4) [perdir /var/www/vhosts/domain.tld/html/bookings/] RewriteCond: input='/var/www/vhosts/domain.tld/html/bookings/index.php' pattern='!-f' => not-matched
105.237.x.x - - [16/May/2014:10:11:18 +0200] [www.domain.tld/sid#7f33674c04f0][rid#7f336731c0a0/subreq] (1) [perdir /var/www/vhosts/domain.tld/html/bookings/] pass through /var/www/vhosts/domain.tld/html/bookings/index.php
As can be seen above, the new server is doing the initial redirect correctly but then there's the extra section that always returns index.php. Any idea what could be causing this? I'm not even 100% sure that they problem is with mod_rewrite. I've run out of options and I don't know much about mod_rewrite

I have no idea what caused this but I reimaged the server, reinstalled the services and the problem went away.

Related

Restler not loading any classes

Using Restler 3.0.0-RC6 I'm getting a 404 from Routes.php at route stage. I've turned on rewrite debugging on apache and I see that it's redirecting to /var/www/html/api/public/index.php when I try to load the URL, and that is in fact where the index.php for Restler lives. It's just not loading any of the classes from the src directory though. How do I debug this?
Here's the rewrite rule output:
[perdir /var/www/html/api/] add path info postfix: /var/www/html/api/v2 -> /var/www/html/api/v2/team/details/1/0
[perdir /var/www/html/api/] strip per-dir prefix: /var/www/html/api/v2/team/details/1/0 -> v2/team/details/1/0
[perdir /var/www/html/api/] applying pattern '^$' to uri 'v2/team/details/1/0'
[perdir /var/www/html/api/] add path info postfix: /var/www/html/api/v2 -> /var/www/html/api/v2/team/details/1/0
[perdir /var/www/html/api/] strip per-dir prefix: /var/www/html/api/v2/team/details/1/0 -> v2/team/details/1/0
[perdir /var/www/html/api/] applying pattern '^(.*)$' to uri 'v2/team/details/1/0'
[perdir /var/www/html/api/] RewriteCond: input='/var/www/html/api/v2' pattern='!-f' => matched
[perdir /var/www/html/api/] RewriteCond: input='/var/www/html/api/v2' pattern='!-d' => matched
[perdir /var/www/html/api/] rewrite 'v2/team/details/1/0' -> 'public/index.php'
[perdir /var/www/html/api/] add per-dir prefix: public/index.php -> /var/www/html/api/public/index.php
[perdir /var/www/html/api/] strip document_root prefix: /var/www/html/api/public/index.php -> /api/public/index.php
[perdir /var/www/html/api/] internal redirect with /api/public/index.php [INTERNAL REDIRECT]
[perdir /var/www/html/api/public/] strip per-dir prefix: /var/www/html/api/public/index.php -> index.php
[perdir /var/www/html/api/public/] applying pattern '^' to uri 'index.php'
[perdir /var/www/html/api/public/] RewriteCond: input='/var/www/html/api/public/index.php' pattern='!-d' => matched
[perdir /var/www/html/api/public/] RewriteCond: input='/var/www/html/api/public/index.php' pattern='!-f' => not-matched
[perdir /var/www/html/api/public/] pass through /var/www/html/api/public/index.php
And as you can see, the index.php is there, and the appropriate class is there:
% ls -l /var/www/html/api/public/index.php
-rw-r--r-- 1 root root 961 Jun 3 21:40 /var/www/html/api/public/index.php
% ls -l /var/www/html/api/src/v2/Team.php
-rw-rwxr-- 1 559 559 19255 May 9 23:02 /var/www/html/api/src/v2/Team.php
I forgot to include the Defaults::$useUrlBasedVersioning = true; line in my index.php so it was always trying to force a v1 version to load, which doesn't exist any longer.

Using placeholder in RewriteCond to be applied to RewriteRule

I am using a placeholder in the RewriteCond which I expected to populate $1, and a second placeholder in the RewriteRule which I expected to populate $2.
It appears, however, that $1 is the placeholder set by the RewriteRule, and $2 is empty.
How can a placeholder be defined in the RewriteCond and applied to the RewriteRule?
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(.+).sites.example.com [NC]
RewriteRule ^public/(.+)$ public/$1/$2 [NC,L]
rewrite log:
[test.sites.example.com/sid#7f4a2b120f18][rid#7f4a2b43cbf8/initial] (3) [perdir /var/www/mysite/html_sites/] strip per-dir prefix: /var/www/mysite/html_sites/public/xxx.html -> public/xxx.html
[test.sites.example.com/sid#7f4a2b120f18][rid#7f4a2b43cbf8/initial] (3) [perdir /var/www/mysite/html_sites/] applying pattern '^public/(.+)$' to uri 'public/xxx.html'
[test.sites.example.com/sid#7f4a2b120f18][rid#7f4a2b43cbf8/initial] (4) [perdir /var/www/mysite/html_sites/] RewriteCond: input='test.sites.example.com' pattern='^(.+).sites.example.com' [NC] => matched
[test.sites.example.com/sid#7f4a2b120f18][rid#7f4a2b43cbf8/initial] (2) [perdir /var/www/mysite/html_sites/] rewrite 'public/xxx.html' -> 'public/xxx.html/'
[test.sites.example.com/sid#7f4a2b120f18][rid#7f4a2b43cbf8/initial] (3) [perdir /var/www/mysite/html_sites/] add per-dir prefix: public/xxx.html/ -> /var/www/mysite/html_sites/public/xxx.html/
[test.sites.example.com/sid#7f4a2b120f18][rid#7f4a2b43cbf8/initial] (2) [perdir /var/www/mysite/html_sites/] trying to replace prefix /var/www/mysite/html_sites/ with /
[test.sites.example.com/sid#7f4a2b120f18][rid#7f4a2b43cbf8/initial] (5) strip matching prefix: /var/www/mysite/html_sites/public/xxx.html/ -> public/xxx.html/
[test.sites.example.com/sid#7f4a2b120f18][rid#7f4a2b43cbf8/initial] (4) add subst prefix: public/xxx.html/ -> /public/xxx.html/
[test.sites.example.com/sid#7f4a2b120f18][rid#7f4a2b43cbf8/initial] (1) [perdir /var/www/mysite/html_sites/] internal redirect with /public/xxx.html/ [INTERNAL REDIRECT]
[test.sites.example.com/sid#7f4a2b120f18][rid#7f4a2b4547b8/initial/redir#1] (3) [perdir /var/www/mysite/html_sites/] add path info postfix: /var/www/mysite/html_sites/public/xxx.html -> /var/www/mysite/html_sites/public/xxx.html/
[test.sites.example.com/sid#7f4a2b120f18][rid#7f4a2b4547b8/initial/redir#1] (3) [perdir /var/www/mysite/html_sites/] strip per-dir prefix: /var/www/mysite/html_sites/public/xxx.html/ -> public/xxx.html/
[test.sites.example.com/sid#7f4a2b120f18][rid#7f4a2b4547b8/initial/redir#1] (3) [perdir /var/www/mysite/html_sites/] applying pattern '^public/(.+)$' to uri 'public/xxx.html/'
[test.sites.example.com/sid#7f4a2b120f18][rid#7f4a2b4547b8/initial/redir#1] (4) [perdir /var/www/mysite/html_sites/] RewriteCond: input='test.sites.example.com' pattern='^(.+).sites.example.com' [NC] => matched
[test.sites.example.com/sid#7f4a2b120f18][rid#7f4a2b4547b8/initial/redir#1] (2) [perdir /var/www/mysite/html_sites/] rewrite 'public/xxx.html/' -> 'public/xxx.html//'
[test.sites.example.com/sid#7f4a2b120f18][rid#7f4a2b4547b8/initial/redir#1] (3) [perdir /var/www/mysite/html_sites/] add per-dir prefix: public/xxx.html// -> /var/www/mysite/html_sites/public/xxx.html//
[test.sites.example.com/sid#7f4a2b120f18][rid#7f4a2b4547b8/initial/redir#1] (2) [perdir /var/www/mysite/html_sites/] trying to replace prefix /var/www/mysite/html_sites/ with /
[test.sites.example.com/sid#7f4a2b120f18][rid#7f4a2b4547b8/initial/redir#1] (5) strip matching prefix: /var/www/mysite/html_sites/public/xxx.html// -> public/xxx.html//
[test.sites.example.com/sid#7f4a2b120f18][rid#7f4a2b4547b8/initial/redir#1] (4) add subst prefix: public/xxx.html// -> /public/xxx.html//
[test.sites.example.com/sid#7f4a2b120f18][rid#7f4a2b4547b8/initial/redir#1] (1) [perdir /var/www/mysite/html_sites/] internal redirect with /public/xxx.html// [INTERNAL REDIRECT]
[test.sites.example.com/sid#7f4a2b120f18][rid#7f4a2b459968/initial/redir#2] (3) [perdir /var/www/mysite/html_sites/] add path info postfix: /var/www/mysite/html_sites/public/xxx.html -> /var/www/mysite/html_sites/public/xxx.html/
[test.sites.example.com/sid#7f4a2b120f18][rid#7f4a2b459968/initial/redir#2] (3) [perdir /var/www/mysite/html_sites/] strip per-dir prefix: /var/www/mysite/html_sites/public/xxx.html/ -> public/xxx.html/
[test.sites.example.com/sid#7f4a2b120f18][rid#7f4a2b459968/initial/redir#2] (3) [perdir /var/www/mysite/html_sites/] applying pattern '^public/(.+)$' to uri 'public/xxx.html/'
[test.sites.example.com/sid#7f4a2b120f18][rid#7f4a2b459968/initial/redir#2] (4) [perdir /var/www/mysite/html_sites/] RewriteCond: input='test.sites.example.com' pattern='^(.+).sites.example.com' [NC] => matched
[test.sites.example.com/sid#7f4a2b120f18][rid#7f4a2b459968/initial/redir#2] (2) [perdir /var/www/mysite/html_sites/] rewrite 'public/xxx.html/' -> 'public/xxx.html//'
[test.sites.example.com/sid#7f4a2b120f18][rid#7f4a2b459968/initial/redir#2] (3) [perdir /var/www/mysite/html_sites/] add per-dir prefix: public/xxx.html// -> /var/www/mysite/html_sites/public/xxx.html//
[test.sites.example.com/sid#7f4a2b120f18][rid#7f4a2b459968/initial/redir#2] (2) [perdir /var/www/mysite/html_sites/] trying to replace prefix /var/www/mysite/html_sites/ with /
[test.sites.example.com/sid#7f4a2b120f18][rid#7f4a2b459968/initial/redir#2] (5) strip matching prefix: /var/www/mysite/html_sites/public/xxx.html// -> public/xxx.html//
[test.sites.example.com/sid#7f4a2b120f18][rid#7f4a2b459968/initial/redir#2] (4) add subst prefix: public/xxx.html// -> /public/xxx.html//
[test.sites.example.com/sid#7f4a2b120f18][rid#7f4a2b459968/initial/redir#2] (1) [perdir /var/www/mysite/html_sites/] internal redirect with /public/xxx.html// [INTERNAL REDIRECT]
[test.sites.example.com/sid#7f4a2b120f18][rid#7f4a2b45eb88/initial/redir#3] (3) [perdir /var/www/mysite/html_sites/] add path info postfix: /var/www/mysite/html_sites/public/xxx.html -> /var/www/mysite/html_sites/public/xxx.html/
[test.sites.example.com/sid#7f4a2b120f18][rid#7f4a2b45eb88/initial/redir#3] (3) [perdir /var/www/mysite/html_sites/] strip per-dir prefix: /var/www/mysite/html_sites/public/xxx.html/ -> public/xxx.html/
[test.sites.example.com/sid#7f4a2b120f18][rid#7f4a2b45eb88/initial/redir#3] (3) [perdir /var/www/mysite/html_sites/] applying pattern '^public/(.+)$' to uri 'public/xxx.html/'
[test.sites.example.com/sid#7f4a2b120f18][rid#7f4a2b45eb88/initial/redir#3] (4) [perdir /var/www/mysite/html_sites/] RewriteCond: input='test.sites.example.com' pattern='^(.+).sites.example.com' [NC] => matched
[test.sites.example.com/sid#7f4a2b120f18][rid#7f4a2b45eb88/initial/redir#3] (2) [perdir /var/www/mysite/html_sites/] rewrite 'public/xxx.html/' -> 'public/xxx.html//'
[test.sites.example.com/sid#7f4a2b120f18][rid#7f4a2b45eb88/initial/redir#3] (3) [perdir /var/www/mysite/html_sites/] add per-dir prefix: public/xxx.html// -> /var/www/mysite/html_sites/public/xxx.html//
[test.sites.example.com/sid#7f4a2b120f18][rid#7f4a2b45eb88/initial/redir#3] (2) [perdir /var/www/mysite/html_sites/] trying to replace prefix /var/www/mysite/html_sites/ with /
[test.sites.example.com/sid#7f4a2b120f18][rid#7f4a2b45eb88/initial/redir#3] (5) strip matching prefix: /var/www/mysite/html_sites/public/xxx.html// -> public/xxx.html//
[test.sites.example.com/sid#7f4a2b120f18][rid#7f4a2b45eb88/initial/redir#3] (4) add subst prefix: public/xxx.html// -> /public/xxx.html//
[test.sites.example.com/sid#7f4a2b120f18][rid#7f4a2b45eb88/initial/redir#3] (1) [perdir /var/www/mysite/html_sites/] internal redirect with /public/xxx.html// [INTERNAL REDIRECT]
[test.sites.example.com/sid#7f4a2b120f18][rid#7f4a2b463dd0/initial/redir#4] (3) [perdir /var/www/mysite/html_sites/] add path info postfix: /var/www/mysite/html_sites/public/xxx.html -> /var/www/mysite/html_sites/public/xxx.html/
[test.sites.example.com/sid#7f4a2b120f18][rid#7f4a2b463dd0/initial/redir#4] (3) [perdir /var/www/mysite/html_sites/] strip per-dir prefix: /var/www/mysite/html_sites/public/xxx.html/ -> public/xxx.html/
[test.sites.example.com/sid#7f4a2b120f18][rid#7f4a2b463dd0/initial/redir#4] (3) [perdir /var/www/mysite/html_sites/] applying pattern '^public/(.+)$' to uri 'public/xxx.html/'
[test.sites.example.com/sid#7f4a2b120f18][rid#7f4a2b463dd0/initial/redir#4] (4) [perdir /var/www/mysite/html_sites/] RewriteCond: input='test.sites.example.com' pattern='^(.+).sites.example.com' [NC] => matched
[test.sites.example.com/sid#7f4a2b120f18][rid#7f4a2b463dd0/initial/redir#4] (2) [perdir /var/www/mysite/html_sites/] rewrite 'public/xxx.html/' -> 'public/xxx.html//'
[test.sites.example.com/sid#7f4a2b120f18][rid#7f4a2b463dd0/initial/redir#4] (3) [perdir /var/www/mysite/html_sites/] add per-dir prefix: public/xxx.html// -> /var/www/mysite/html_sites/public/xxx.html//
[test.sites.example.com/sid#7f4a2b120f18][rid#7f4a2b463dd0/initial/redir#4] (2) [perdir /var/www/mysite/html_sites/] trying to replace prefix /var/www/mysite/html_sites/ with /
[test.sites.example.com/sid#7f4a2b120f18][rid#7f4a2b463dd0/initial/redir#4] (5) strip matching prefix: /var/www/mysite/html_sites/public/xxx.html// -> public/xxx.html//
[test.sites.example.com/sid#7f4a2b120f18][rid#7f4a2b463dd0/initial/redir#4] (4) add subst prefix: public/xxx.html// -> /public/xxx.html//
[test.sites.example.com/sid#7f4a2b120f18][rid#7f4a2b463dd0/initial/redir#4] (1) [perdir /var/www/mysite/html_sites/] internal redirect with /public/xxx.html// [INTERNAL REDIRECT]
[test.sites.example.com/sid#7f4a2b120f18][rid#7f4a2b469218/initial/redir#5] (3) [perdir /var/www/mysite/html_sites/] add path info postfix: /var/www/mysite/html_sites/public/xxx.html -> /var/www/mysite/html_sites/public/xxx.html/
[test.sites.example.com/sid#7f4a2b120f18][rid#7f4a2b469218/initial/redir#5] (3) [perdir /var/www/mysite/html_sites/] strip per-dir prefix: /var/www/mysite/html_sites/public/xxx.html/ -> public/xxx.html/
[test.sites.example.com/sid#7f4a2b120f18][rid#7f4a2b469218/initial/redir#5] (3) [perdir /var/www/mysite/html_sites/] applying pattern '^public/(.+)$' to uri 'public/xxx.html/'
[test.sites.example.com/sid#7f4a2b120f18][rid#7f4a2b469218/initial/redir#5] (4) [perdir /var/www/mysite/html_sites/] RewriteCond: input='test.sites.example.com' pattern='^(.+).sites.example.com' [NC] => matched
[test.sites.example.com/sid#7f4a2b120f18][rid#7f4a2b469218/initial/redir#5] (2) [perdir /var/www/mysite/html_sites/] rewrite 'public/xxx.html/' -> 'public/xxx.html//'
[test.sites.example.com/sid#7f4a2b120f18][rid#7f4a2b469218/initial/redir#5] (3) [perdir /var/www/mysite/html_sites/] add per-dir prefix: public/xxx.html// -> /var/www/mysite/html_sites/public/xxx.html//
[test.sites.example.com/sid#7f4a2b120f18][rid#7f4a2b469218/initial/redir#5] (2) [perdir /var/www/mysite/html_sites/] trying to replace prefix /var/www/mysite/html_sites/ with /
[test.sites.example.com/sid#7f4a2b120f18][rid#7f4a2b469218/initial/redir#5] (5) strip matching prefix: /var/www/mysite/html_sites/public/xxx.html// -> public/xxx.html//
[test.sites.example.com/sid#7f4a2b120f18][rid#7f4a2b469218/initial/redir#5] (4) add subst prefix: public/xxx.html// -> /public/xxx.html//
[test.sites.example.com/sid#7f4a2b120f18][rid#7f4a2b469218/initial/redir#5] (1) [perdir /var/www/mysite/html_sites/] internal redirect with /public/xxx.html// [INTERNAL REDIRECT]
[test.sites.example.com/sid#7f4a2b120f18][rid#7f4a2b46e628/initial/redir#6] (3) [perdir /var/www/mysite/html_sites/] add path info postfix: /var/www/mysite/html_sites/public/xxx.html -> /var/www/mysite/html_sites/public/xxx.html/
[test.sites.example.com/sid#7f4a2b120f18][rid#7f4a2b46e628/initial/redir#6] (3) [perdir /var/www/mysite/html_sites/] strip per-dir prefix: /var/www/mysite/html_sites/public/xxx.html/ -> public/xxx.html/
[test.sites.example.com/sid#7f4a2b120f18][rid#7f4a2b46e628/initial/redir#6] (3) [perdir /var/www/mysite/html_sites/] applying pattern '^public/(.+)$' to uri 'public/xxx.html/'
[test.sites.example.com/sid#7f4a2b120f18][rid#7f4a2b46e628/initial/redir#6] (4) [perdir /var/www/mysite/html_sites/] RewriteCond: input='test.sites.example.com' pattern='^(.+).sites.example.com' [NC] => matched
[test.sites.example.com/sid#7f4a2b120f18][rid#7f4a2b46e628/initial/redir#6] (2) [perdir /var/www/mysite/html_sites/] rewrite 'public/xxx.html/' -> 'public/xxx.html//'
[test.sites.example.com/sid#7f4a2b120f18][rid#7f4a2b46e628/initial/redir#6] (3) [perdir /var/www/mysite/html_sites/] add per-dir prefix: public/xxx.html// -> /var/www/mysite/html_sites/public/xxx.html//
[test.sites.example.com/sid#7f4a2b120f18][rid#7f4a2b46e628/initial/redir#6] (2) [perdir /var/www/mysite/html_sites/] trying to replace prefix /var/www/mysite/html_sites/ with /
[test.sites.example.com/sid#7f4a2b120f18][rid#7f4a2b46e628/initial/redir#6] (5) strip matching prefix: /var/www/mysite/html_sites/public/xxx.html// -> public/xxx.html//
[test.sites.example.com/sid#7f4a2b120f18][rid#7f4a2b46e628/initial/redir#6] (4) add subst prefix: public/xxx.html// -> /public/xxx.html//
[test.sites.example.com/sid#7f4a2b120f18][rid#7f4a2b46e628/initial/redir#6] (1) [perdir /var/www/mysite/html_sites/] internal redirect with /public/xxx.html// [INTERNAL REDIRECT]
[test.sites.example.com/sid#7f4a2b120f18][rid#7f4a2b473d88/initial/redir#7] (3) [perdir /var/www/mysite/html_sites/] add path info postfix: /var/www/mysite/html_sites/public/xxx.html -> /var/www/mysite/html_sites/public/xxx.html/
[test.sites.example.com/sid#7f4a2b120f18][rid#7f4a2b473d88/initial/redir#7] (3) [perdir /var/www/mysite/html_sites/] strip per-dir prefix: /var/www/mysite/html_sites/public/xxx.html/ -> public/xxx.html/
[test.sites.example.com/sid#7f4a2b120f18][rid#7f4a2b473d88/initial/redir#7] (3) [perdir /var/www/mysite/html_sites/] applying pattern '^public/(.+)$' to uri 'public/xxx.html/'
[test.sites.example.com/sid#7f4a2b120f18][rid#7f4a2b473d88/initial/redir#7] (4) [perdir /var/www/mysite/html_sites/] RewriteCond: input='test.sites.example.com' pattern='^(.+).sites.example.com' [NC] => matched
[test.sites.example.com/sid#7f4a2b120f18][rid#7f4a2b473d88/initial/redir#7] (2) [perdir /var/www/mysite/html_sites/] rewrite 'public/xxx.html/' -> 'public/xxx.html//'
[test.sites.example.com/sid#7f4a2b120f18][rid#7f4a2b473d88/initial/redir#7] (3) [perdir /var/www/mysite/html_sites/] add per-dir prefix: public/xxx.html// -> /var/www/mysite/html_sites/public/xxx.html//
[test.sites.example.com/sid#7f4a2b120f18][rid#7f4a2b473d88/initial/redir#7] (2) [perdir /var/www/mysite/html_sites/] trying to replace prefix /var/www/mysite/html_sites/ with /
[test.sites.example.com/sid#7f4a2b120f18][rid#7f4a2b473d88/initial/redir#7] (5) strip matching prefix: /var/www/mysite/html_sites/public/xxx.html// -> public/xxx.html//
[test.sites.example.com/sid#7f4a2b120f18][rid#7f4a2b473d88/initial/redir#7] (4) add subst prefix: public/xxx.html// -> /public/xxx.html//
[test.sites.example.com/sid#7f4a2b120f18][rid#7f4a2b473d88/initial/redir#7] (1) [perdir /var/www/mysite/html_sites/] internal redirect with /public/xxx.html// [INTERNAL REDIRECT]
[test.sites.example.com/sid#7f4a2b120f18][rid#7f4a2b4792d8/initial/redir#8] (3) [perdir /var/www/mysite/html_sites/] add path info postfix: /var/www/mysite/html_sites/public/xxx.html -> /var/www/mysite/html_sites/public/xxx.html/
[test.sites.example.com/sid#7f4a2b120f18][rid#7f4a2b4792d8/initial/redir#8] (3) [perdir /var/www/mysite/html_sites/] strip per-dir prefix: /var/www/mysite/html_sites/public/xxx.html/ -> public/xxx.html/
[test.sites.example.com/sid#7f4a2b120f18][rid#7f4a2b4792d8/initial/redir#8] (3) [perdir /var/www/mysite/html_sites/] applying pattern '^public/(.+)$' to uri 'public/xxx.html/'
[test.sites.example.com/sid#7f4a2b120f18][rid#7f4a2b4792d8/initial/redir#8] (4) [perdir /var/www/mysite/html_sites/] RewriteCond: input='test.sites.example.com' pattern='^(.+).sites.example.com' [NC] => matched
[test.sites.example.com/sid#7f4a2b120f18][rid#7f4a2b4792d8/initial/redir#8] (2) [perdir /var/www/mysite/html_sites/] rewrite 'public/xxx.html/' -> 'public/xxx.html//'
[test.sites.example.com/sid#7f4a2b120f18][rid#7f4a2b4792d8/initial/redir#8] (3) [perdir /var/www/mysite/html_sites/] add per-dir prefix: public/xxx.html// -> /var/www/mysite/html_sites/public/xxx.html//
[test.sites.example.com/sid#7f4a2b120f18][rid#7f4a2b4792d8/initial/redir#8] (2) [perdir /var/www/mysite/html_sites/] trying to replace prefix /var/www/mysite/html_sites/ with /
[test.sites.example.com/sid#7f4a2b120f18][rid#7f4a2b4792d8/initial/redir#8] (5) strip matching prefix: /var/www/mysite/html_sites/public/xxx.html// -> public/xxx.html//
[test.sites.example.com/sid#7f4a2b120f18][rid#7f4a2b4792d8/initial/redir#8] (4) add subst prefix: public/xxx.html// -> /public/xxx.html//
[test.sites.example.com/sid#7f4a2b120f18][rid#7f4a2b4792d8/initial/redir#8] (1) [perdir /var/www/mysite/html_sites/] internal redirect with /public/xxx.html// [INTERNAL REDIRECT]
[test.sites.example.com/sid#7f4a2b120f18][rid#7f4a2b47f498/initial/redir#9] (3) [perdir /var/www/mysite/html_sites/] add path info postfix: /var/www/mysite/html_sites/public/xxx.html -> /var/www/mysite/html_sites/public/xxx.html/
[test.sites.example.com/sid#7f4a2b120f18][rid#7f4a2b47f498/initial/redir#9] (3) [perdir /var/www/mysite/html_sites/] strip per-dir prefix: /var/www/mysite/html_sites/public/xxx.html/ -> public/xxx.html/
[test.sites.example.com/sid#7f4a2b120f18][rid#7f4a2b47f498/initial/redir#9] (3) [perdir /var/www/mysite/html_sites/] applying pattern '^public/(.+)$' to uri 'public/xxx.html/'
[test.sites.example.com/sid#7f4a2b120f18][rid#7f4a2b47f498/initial/redir#9] (4) [perdir /var/www/mysite/html_sites/] RewriteCond: input='test.sites.example.com' pattern='^(.+).sites.example.com' [NC] => matched
[test.sites.example.com/sid#7f4a2b120f18][rid#7f4a2b47f498/initial/redir#9] (2) [perdir /var/www/mysite/html_sites/] rewrite 'public/xxx.html/' -> 'public/xxx.html//'
[test.sites.example.com/sid#7f4a2b120f18][rid#7f4a2b47f498/initial/redir#9] (3) [perdir /var/www/mysite/html_sites/] add per-dir prefix: public/xxx.html// -> /var/www/mysite/html_sites/public/xxx.html//
[test.sites.example.com/sid#7f4a2b120f18][rid#7f4a2b47f498/initial/redir#9] (2) [perdir /var/www/mysite/html_sites/] trying to replace prefix /var/www/mysite/html_sites/ with /
[test.sites.example.com/sid#7f4a2b120f18][rid#7f4a2b47f498/initial/redir#9] (5) strip matching prefix: /var/www/mysite/html_sites/public/xxx.html// -> public/xxx.html//
[test.sites.example.com/sid#7f4a2b120f18][rid#7f4a2b47f498/initial/redir#9] (4) add subst prefix: public/xxx.html// -> /public/xxx.html//
[test.sites.example.com/sid#7f4a2b120f18][rid#7f4a2b47f498/initial/redir#9] (1) [perdir /var/www/mysite/html_sites/] internal redirect with /public/xxx.html// [INTERNAL REDIRECT]
[test.sites.example.com/sid#7f4a2b120f18][rid#7f4a2b484cb8/initial/redir#10] (3) [perdir /var/www/mysite/html_sites/] add path info postfix: /var/www/mysite/html_sites/public/xxx.html -> /var/www/mysite/html_sites/public/xxx.html/
[test.sites.example.com/sid#7f4a2b120f18][rid#7f4a2b484cb8/initial/redir#10] (3) [perdir /var/www/mysite/html_sites/] strip per-dir prefix: /var/www/mysite/html_sites/public/xxx.html/ -> public/xxx.html/
[test.sites.example.com/sid#7f4a2b120f18][rid#7f4a2b484cb8/initial/redir#10] (3) [perdir /var/www/mysite/html_sites/] applying pattern '^public/(.+)$' to uri 'public/xxx.html/'
[test.sites.example.com/sid#7f4a2b120f18][rid#7f4a2b484cb8/initial/redir#10] (4) [perdir /var/www/mysite/html_sites/] RewriteCond: input='test.sites.example.com' pattern='^(.+).sites.example.com' [NC] => matched
[test.sites.example.com/sid#7f4a2b120f18][rid#7f4a2b484cb8/initial/redir#10] (2) [perdir /var/www/mysite/html_sites/] rewrite 'public/xxx.html/' -> 'public/xxx.html//'
[test.sites.example.com/sid#7f4a2b120f18][rid#7f4a2b484cb8/initial/redir#10] (3) [perdir /var/www/mysite/html_sites/] add per-dir prefix: public/xxx.html// -> /var/www/mysite/html_sites/public/xxx.html//
[test.sites.example.com/sid#7f4a2b120f18][rid#7f4a2b484cb8/initial/redir#10] (2) [perdir /var/www/mysite/html_sites/] trying to replace prefix /var/www/mysite/html_sites/ with /
[test.sites.example.com/sid#7f4a2b120f18][rid#7f4a2b484cb8/initial/redir#10] (5) strip matching prefix: /var/www/mysite/html_sites/public/xxx.html// -> public/xxx.html//
[test.sites.example.com/sid#7f4a2b120f18][rid#7f4a2b484cb8/initial/redir#10] (4) add subst prefix: public/xxx.html// -> /public/xxx.html//
[test.sites.example.com/sid#7f4a2b120f18][rid#7f4a2b484cb8/initial/redir#10] (1) [perdir /var/www/mysite/html_sites/] internal redirect with /public/xxx.html// [INTERNAL REDIRECT]
[test.sites.example.com/sid#7f4a2b120f18][rid#7f4a2b43cbf8/initial] (3) [perdir /var/www/mysite/html_sites/] strip per-dir prefix: /var/www/mysite/html_sites/favicon.ico -> favicon.ico
[test.sites.example.com/sid#7f4a2b120f18][rid#7f4a2b43cbf8/initial] (3) [perdir /var/www/mysite/html_sites/] applying pattern '^public/(.+)$' to uri 'favicon.ico'
[test.sites.example.com/sid#7f4a2b120f18][rid#7f4a2b43cbf8/initial] (1) [perdir /var/www/mysite/html_sites/] pass through /var/www/mysite/html_sites/favicon.ico
Placeholders for RewriteCond use % and placeholders for RewriteRule use $. Change to...
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(.+).sites.example.com [NC]
RewriteRule ^public/(.+)$ public/%1/$1 [NC,L]

Why Apache mod_rewrite rewrites twice my URL?

I need to rewrite /admin/page/create URL to /backend/www/index.php/page/create, so I use the next Apache rewrite rule and conditions:
RewriteCond %{REQUEST_URI} !-f
RewriteCond %{REQUEST_URI} !-d
RewriteCond %{REQUEST_URI} !backend\/www
RewriteRule admin\/(.*)$ backend/www/index.php/$1 [NS,L]
However, examining the rewrite log, I see Apache rewrites correctly the URL, but at the end it performs an additional rewrite considering /page/create URL, which doesn't match any condition, so it fails and I receive the "No input file specified." error.
How can I prevent Apache to perform that additional rewrite? I used NS flag and RewriteCond %{IS_SUBREQ} f condition and it also performs the subrequest.
Thank you!
80.29.4.44 - - [13/Jun/2013:21:40:54 +0200] [dev.daniloaz.com/sid#7f65e59b6a78][rid#7f65e5a35d60/initial] (2) init rewrite engine with requested uri /admin/page/create
80.29.4.44 - - [13/Jun/2013:21:40:54 +0200] [dev.daniloaz.com/sid#7f65e59b6a78][rid#7f65e5a35d60/initial] (1) pass through /admin/page/create
80.29.4.44 - - [13/Jun/2013:21:40:54 +0200] [dev.daniloaz.com/sid#7f65e59b6a78][rid#7f65e5a35d60/initial] (3) [perdir /home/daniloaz/dev.daniloaz.com/httpdocs/] add path info postfix: /home/daniloaz/dev.daniloaz.com/httpdocs/admin -> /home/daniloaz/dev.daniloaz.com/httpdocs/admin/page/create
80.29.4.44 - - [13/Jun/2013:21:40:54 +0200] [dev.daniloaz.com/sid#7f65e59b6a78][rid#7f65e5a35d60/initial] (3) [perdir /home/daniloaz/dev.daniloaz.com/httpdocs/] strip per-dir prefix: /home/daniloaz/dev.daniloaz.com/httpdocs/admin/page/create -> admin/page/create
80.29.4.44 - - [13/Jun/2013:21:40:54 +0200] [dev.daniloaz.com/sid#7f65e59b6a78][rid#7f65e5a35d60/initial] (3) [perdir /home/daniloaz/dev.daniloaz.com/httpdocs/] applying pattern 'admin\/(.*)$' to uri 'admin/page/create'
80.29.4.44 - - [13/Jun/2013:21:40:54 +0200] [dev.daniloaz.com/sid#7f65e59b6a78][rid#7f65e5a35d60/initial] (4) [perdir /home/daniloaz/dev.daniloaz.com/httpdocs/] RewriteCond: input='/admin/page/create' pattern='!-f' => matched
80.29.4.44 - - [13/Jun/2013:21:40:54 +0200] [dev.daniloaz.com/sid#7f65e59b6a78][rid#7f65e5a35d60/initial] (4) [perdir /home/daniloaz/dev.daniloaz.com/httpdocs/] RewriteCond: input='/admin/page/create' pattern='!-d' => matched
80.29.4.44 - - [13/Jun/2013:21:40:54 +0200] [dev.daniloaz.com/sid#7f65e59b6a78][rid#7f65e5a35d60/initial] (4) [perdir /home/daniloaz/dev.daniloaz.com/httpdocs/] RewriteCond: input='/admin/page/create' pattern='!backend\/www' => matched
80.29.4.44 - - [13/Jun/2013:21:40:54 +0200] [dev.daniloaz.com/sid#7f65e59b6a78][rid#7f65e5a35d60/initial] (4) [perdir /home/daniloaz/dev.daniloaz.com/httpdocs/] RewriteCond: input='/admin/page/create' pattern='((admin\/(.*)$)|(admin$))' => matched
80.29.4.44 - - [13/Jun/2013:21:40:54 +0200] [dev.daniloaz.com/sid#7f65e59b6a78][rid#7f65e5a35d60/initial] (4) [perdir /home/daniloaz/dev.daniloaz.com/httpdocs/] RewriteCond: input='false' pattern='f' => matched
80.29.4.44 - - [13/Jun/2013:21:40:54 +0200] [dev.daniloaz.com/sid#7f65e59b6a78][rid#7f65e5a35d60/initial] (2) [perdir /home/daniloaz/dev.daniloaz.com/httpdocs/] rewrite 'admin/page/create' -> 'backend/www/index.php/page/create'
80.29.4.44 - - [13/Jun/2013:21:40:54 +0200] [dev.daniloaz.com/sid#7f65e59b6a78][rid#7f65e5a35d60/initial] (3) [perdir /home/daniloaz/dev.daniloaz.com/httpdocs/] add per-dir prefix: backend/www/index.php/page/create -> /home/daniloaz/dev.daniloaz.com/httpdocs/backend/www/index.php/page/create
80.29.4.44 - - [13/Jun/2013:21:40:54 +0200] [dev.daniloaz.com/sid#7f65e59b6a78][rid#7f65e5a35d60/initial] (2) [perdir /home/daniloaz/dev.daniloaz.com/httpdocs/] strip document_root prefix: /home/daniloaz/dev.daniloaz.com/httpdocs/backend/www/index.php/page/create -> /backend/www/index.php/page/create
80.29.4.44 - - [13/Jun/2013:21:40:54 +0200] [dev.daniloaz.com/sid#7f65e59b6a78][rid#7f65e5a35d60/initial] (1) [perdir /home/daniloaz/dev.daniloaz.com/httpdocs/] internal redirect with /backend/www/index.php/page/create [INTERNAL REDIRECT]
80.29.4.44 - - [13/Jun/2013:21:40:54 +0200] [dev.daniloaz.com/sid#7f65e59b6a78][rid#7f65e5a472f0/initial/redir#1] (2) init rewrite engine with requested uri /backend/www/index.php/page/create
80.29.4.44 - - [13/Jun/2013:21:40:54 +0200] [dev.daniloaz.com/sid#7f65e59b6a78][rid#7f65e5a472f0/initial/redir#1] (1) pass through /backend/www/index.php/page/create
80.29.4.44 - - [13/Jun/2013:21:40:54 +0200] [dev.daniloaz.com/sid#7f65e59b6a78][rid#7f65e5a472f0/initial/redir#1] (3) [perdir /home/daniloaz/dev.daniloaz.com/httpdocs/] add path info postfix: /home/daniloaz/dev.daniloaz.com/httpdocs/backend/www/index.php -> /home/daniloaz/dev.daniloaz.com/httpdocs/backend/www/index.php/page/create
80.29.4.44 - - [13/Jun/2013:21:40:54 +0200] [dev.daniloaz.com/sid#7f65e59b6a78][rid#7f65e5a472f0/initial/redir#1] (3) [perdir /home/daniloaz/dev.daniloaz.com/httpdocs/] strip per-dir prefix: /home/daniloaz/dev.daniloaz.com/httpdocs/backend/www/index.php/page/create -> backend/www/index.php/page/create
80.29.4.44 - - [13/Jun/2013:21:40:54 +0200] [dev.daniloaz.com/sid#7f65e59b6a78][rid#7f65e5a472f0/initial/redir#1] (3) [perdir /home/daniloaz/dev.daniloaz.com/httpdocs/] applying pattern 'admin\/(.*)$' to uri 'backend/www/index.php/page/create'
80.29.4.44 - - [13/Jun/2013:21:40:54 +0200] [dev.daniloaz.com/sid#7f65e59b6a78][rid#7f65e5a472f0/initial/redir#1] (1) [perdir /home/daniloaz/dev.daniloaz.com/httpdocs/] pass through /home/daniloaz/dev.daniloaz.com/httpdocs/backend/www/index.php
80.29.4.44 - - [13/Jun/2013:21:40:54 +0200] [dev.daniloaz.com/sid#7f65e59b6a78][rid#7f65e5a4ce60/subreq] (2) init rewrite engine with requested uri /page/create
80.29.4.44 - - [13/Jun/2013:21:40:54 +0200] [dev.daniloaz.com/sid#7f65e59b6a78][rid#7f65e5a4ce60/subreq] (1) pass through /page/create
80.29.4.44 - - [13/Jun/2013:21:40:54 +0200] [dev.daniloaz.com/sid#7f65e59b6a78][rid#7f65e5a4ce60/subreq] (1) [perdir /home/daniloaz/dev.daniloaz.com/httpdocs/] pass through /home/daniloaz/dev.daniloaz.com/httpdocs/page
Well, I finally solved this issue using a workaround. First I tried these solutions, but subrequests were performed again and again and rewriting failed as before.
With mod_rewrite, there are small differences if you put a rule in .htaccess or in global configuration (httpd.conf, Virtualhost configuration, etc.). Subrequests are one of that differences. Just place the same rule I mentioned above in Virtualhost config file instead of .htaccess and subqueries stop and everything starts working like a charm.
I recommend this useful post to fully understand the internals of mod_rewrite before trying to solve any related to it issue: http://net.tutsplus.com/tutorials/other/a-deeper-look-at-mod_rewrite-for-apache/
I just want to drop my experience here in case somebody reaches the same point I did:
I had the same problem. I tried every single answer. Every single solution. Up to the level when I can say "I really fully understand the rewrite engine". But nothing works. My URLs were still changing and showing the query string.
In some moment I said: "This is likely improbable. Must be something else". And then I tried to get the URL with WGET instead of the browser (firefox).
I did:
wget -qSO http://MY-THING
And then I realized that NOTHING from the server said the URL I saw in the browser's address bar. And even better, NOTHING in the log file!
So... how and why does it change? The obvious answer is "the browser"
I tried another browser (chrome) and works!
So what I did was to clean the history with the "forget this site" option and everybody happy.
Now I don't know WHEN I fixed it. Might be the -Multiviews thing, or the rule's flags, or the INTERNAL REDIRECT part... I don't really know. But probably the solution wasn't there.

Apache rewrite parses URL twice unexpectedly

I'm trying to redirect all the webpages from certain domain names to a specific web application that handles several sites.
I have my web app in a folder "skylight2" and another folder, named "testsite", with only a .htaccess file:
RewriteEngine on
RewriteRule ^(((?!skylight2).)*)$ /home/stefano/webroot_aliases/skylight2/$1/?site=testsite [QSA]
I'm using virtual roots, and my httpd-vroot.conf looks like this:
<VirtualHost *:80>
ServerName webserver
ServerAlias *.webserver
UseCanonicalName Off
VirtualDocumentRoot /home/stefano/webroot_aliases/%1
Options -Indexes FollowSymLinks -MultiViews
RewriteLog "/var/log/httpd/vroot_rewrite.log"
RewriteLogLevel 3
<Directory /home/stefano/webroot_aliases/>
Order allow,deny
Allow from all
AllowOverride FileInfo Indexes Limit Options
RewriteEngine On
RewriteBase /
</Directory>
</VirtualHost>
If I try to access testsite.webserver, I get an "object not found" error. This is my rewrite log:
127.0.0.1 - - [08/Mar/2012:07:56:23 --0600] [testsite.webserver/sid#21f2ca0][rid#253f320/initial] (3) [perdir /home/stefano/webroot_aliases/testsite/] add path info postfix: /home/stefano/webroot_aliases/testsite/articles -> /home/stefano/webroot_aliases/testsite/articles/view
127.0.0.1 - - [08/Mar/2012:07:56:23 --0600] [testsite.webserver/sid#21f2ca0][rid#253f320/initial] (3) [perdir /home/stefano/webroot_aliases/testsite/] strip per-dir prefix: /home/stefano/webroot_aliases/testsite/articles/view -> articles/view
127.0.0.1 - - [08/Mar/2012:07:56:23 --0600] [testsite.webserver/sid#21f2ca0][rid#253f320/initial] (3) [perdir /home/stefano/webroot_aliases/testsite/] applying pattern '^(((?!skylight2).)*)$' to uri 'articles/view'
127.0.0.1 - - [08/Mar/2012:07:56:23 --0600] [testsite.webserver/sid#21f2ca0][rid#253f320/initial] (2) [perdir /home/stefano/webroot_aliases/testsite/] rewrite 'articles/view' -> '/home/stefano/webroot_aliases/skylight2/articles/view/?site=testsite'
127.0.0.1 - - [08/Mar/2012:07:56:23 --0600] [testsite.webserver/sid#21f2ca0][rid#253f320/initial] (3) split uri=/home/stefano/webroot_aliases/skylight2/articles/view/?site=testsite -> uri=/home/stefano/webroot_aliases/skylight2/articles/view/, args=site=testsite
127.0.0.1 - - [08/Mar/2012:07:56:23 --0600] [testsite.webserver/sid#21f2ca0][rid#253f320/initial] (1) [perdir /home/stefano/webroot_aliases/testsite/] internal redirect with /home/stefano/webroot_aliases/skylight2/articles/view/ [INTERNAL REDIRECT]
127.0.0.1 - - [08/Mar/2012:07:56:23 --0600] [testsite.webserver/sid#21f2ca0][rid#2531a18/initial/redir#1] (3) [perdir /home/stefano/webroot_aliases/testsite/] add path info postfix: /home/stefano/webroot_aliases/testsite/home -> /home/stefano/webroot_aliases/testsite/home/stefano/webroot_aliases/skylight2/articles/view/
127.0.0.1 - - [08/Mar/2012:07:56:23 --0600] [testsite.webserver/sid#21f2ca0][rid#2531a18/initial/redir#1] (3) [perdir /home/stefano/webroot_aliases/testsite/] strip per-dir prefix: /home/stefano/webroot_aliases/testsite/home/stefano/webroot_aliases/skylight2/articles/view/ -> home/stefano/webroot_aliases/skylight2/articles/view/
127.0.0.1 - - [08/Mar/2012:07:56:23 --0600] [testsite.webserver/sid#21f2ca0][rid#2531a18/initial/redir#1] (3) [perdir /home/stefano/webroot_aliases/testsite/] applying pattern '^(((?!skylight2).)*)$' to uri 'home/stefano/webroot_aliases/skylight2/articles/view/'
127.0.0.1 - - [08/Mar/2012:07:56:23 --0600] [testsite.webserver/sid#21f2ca0][rid#2531a18/initial/redir#1] (1) [perdir /home/stefano/webroot_aliases/testsite/] pass through /home/stefano/webroot_aliases/testsite/home
127.0.0.1 - - [08/Mar/2012:07:56:23 --0600] [testsite.webserver/sid#21f2ca0][rid#252f2a0/initial] (3) [perdir /home/stefano/webroot_aliases/testsite/] strip per-dir prefix: /home/stefano/webroot_aliases/testsite/favicon.ico -> favicon.ico
127.0.0.1 - - [08/Mar/2012:07:56:23 --0600] [testsite.webserver/sid#21f2ca0][rid#252f2a0/initial] (3) [perdir /home/stefano/webroot_aliases/testsite/] applying pattern '^(((?!skylight2).)*)$' to uri 'favicon.ico'
127.0.0.1 - - [08/Mar/2012:07:56:23 --0600] [testsite.webserver/sid#21f2ca0][rid#252f2a0/initial] (2) [perdir /home/stefano/webroot_aliases/testsite/] rewrite 'favicon.ico' -> '/home/stefano/webroot_aliases/skylight2/favicon.ico/?site=testsite'
127.0.0.1 - - [08/Mar/2012:07:56:23 --0600] [testsite.webserver/sid#21f2ca0][rid#252f2a0/initial] (3) split uri=/home/stefano/webroot_aliases/skylight2/favicon.ico/?site=testsite -> uri=/home/stefano/webroot_aliases/skylight2/favicon.ico/, args=site=testsite
127.0.0.1 - - [08/Mar/2012:07:56:23 --0600] [testsite.webserver/sid#21f2ca0][rid#252f2a0/initial] (1) [perdir /home/stefano/webroot_aliases/testsite/] internal redirect with /home/stefano/webroot_aliases/skylight2/favicon.ico/ [INTERNAL REDIRECT]
127.0.0.1 - - [08/Mar/2012:07:56:23 --0600] [testsite.webserver/sid#21f2ca0][rid#2533830/initial/redir#1] (3) [perdir /home/stefano/webroot_aliases/testsite/] add path info postfix: /home/stefano/webroot_aliases/testsite/home -> /home/stefano/webroot_aliases/testsite/home/stefano/webroot_aliases/skylight2/favicon.ico/
127.0.0.1 - - [08/Mar/2012:07:56:23 --0600] [testsite.webserver/sid#21f2ca0][rid#2533830/initial/redir#1] (3) [perdir /home/stefano/webroot_aliases/testsite/] strip per-dir prefix: /home/stefano/webroot_aliases/testsite/home/stefano/webroot_aliases/skylight2/favicon.ico/ -> home/stefano/webroot_aliases/skylight2/favicon.ico/
127.0.0.1 - - [08/Mar/2012:07:56:23 --0600] [testsite.webserver/sid#21f2ca0][rid#2533830/initial/redir#1] (3) [perdir /home/stefano/webroot_aliases/testsite/] applying pattern '^(((?!skylight2).)*)$' to uri 'home/stefano/webroot_aliases/skylight2/favicon.ico/'
127.0.0.1 - - [08/Mar/2012:07:56:23 --0600] [testsite.webserver/sid#21f2ca0][rid#2533830/initial/redir#1] (1) [perdir /home/stefano/webroot_aliases/testsite/] pass through /home/stefano/webroot_aliases/testsite/home
Apparently, the URL is parsed once (correctly), and then the second time where it's added the prefix again, resulting in an error. Unfortunately, I cannot put a [L] flag to this rule because the URL gets parsed further in the application folder.
Can anybody explain to me what is happening?
Thanks
gm
EDIT: I applied the rule:
RewriteRule ^(?!skylight2)(.*)$ skylight2/$1/?site=testsite [QSA,L]
And get this:
127.0.0.1 - - [09/Mar/2012:07:22:45 --0600] [testsite.webserver/sid#161fca0][rid#1951280/initial] (3) [perdir /home/stefano/webroot_aliases/testsite/] add path info postfix: /home/stefano/webroot_a
liases/testsite/articles -> /home/stefano/webroot_aliases/testsite/articles/view
127.0.0.1 - - [09/Mar/2012:07:22:45 --0600] [testsite.webserver/sid#161fca0][rid#1951280/initial] (3) [perdir /home/stefano/webroot_aliases/testsite/] strip per-dir prefix: /home/stefano/webroot_al
iases/testsite/articles/view -> articles/view
127.0.0.1 - - [09/Mar/2012:07:22:45 --0600] [testsite.webserver/sid#161fca0][rid#1951280/initial] (3) [perdir /home/stefano/webroot_aliases/testsite/] applying pattern '^(?!skylight2)(.*)$' to uri
'articles/view'
127.0.0.1 - - [09/Mar/2012:07:22:45 --0600] [testsite.webserver/sid#161fca0][rid#1951280/initial] (2) [perdir /home/stefano/webroot_aliases/testsite/] rewrite 'articles/view' -> 'skylight2/articles
/view/?site=testsite'
127.0.0.1 - - [09/Mar/2012:07:22:45 --0600] [testsite.webserver/sid#161fca0][rid#1951280/initial] (3) split uri=skylight2/articles/view/?site=testsite -> uri=skylight2/articles/view/, args=site=tes
tsite
127.0.0.1 - - [09/Mar/2012:07:22:45 --0600] [testsite.webserver/sid#161fca0][rid#1951280/initial] (3) [perdir /home/stefano/webroot_aliases/testsite/] add per-dir prefix: skylight2/articles/view/ -
> /home/stefano/webroot_aliases/testsite/skylight2/articles/view/
127.0.0.1 - - [09/Mar/2012:07:22:45 --0600] [testsite.webserver/sid#161fca0][rid#1951280/initial] (1) [perdir /home/stefano/webroot_aliases/testsite/] internal redirect with /home/stefano/webroot_a
liases/testsite/skylight2/articles/view/ [INTERNAL REDIRECT]
127.0.0.1 - - [09/Mar/2012:07:22:45 --0600] [testsite.webserver/sid#161fca0][rid#195c9e0/initial/redir#1] (3) [perdir /home/stefano/webroot_aliases/testsite/] add path info postfix: /home/stefano/w
ebroot_aliases/testsite/home -> /home/stefano/webroot_aliases/testsite/home/stefano/webroot_aliases/testsite/skylight2/articles/view/
127.0.0.1 - - [09/Mar/2012:07:22:45 --0600] [testsite.webserver/sid#161fca0][rid#195c9e0/initial/redir#1] (3) [perdir /home/stefano/webroot_aliases/testsite/] strip per-dir prefix: /home/stefano/we
broot_aliases/testsite/home/stefano/webroot_aliases/testsite/skylight2/articles/view/ -> home/stefano/webroot_aliases/testsite/skylight2/articles/view/
127.0.0.1 - - [09/Mar/2012:07:22:45 --0600] [testsite.webserver/sid#161fca0][rid#195c9e0/initial/redir#1] (3) [perdir /home/stefano/webroot_aliases/testsite/] applying pattern '^(?!skylight2)(.*)$'
to uri 'home/stefano/webroot_aliases/testsite/skylight2/articles/view/'
127.0.0.1 - - [09/Mar/2012:07:22:45 --0600] [testsite.webserver/sid#161fca0][rid#195c9e0/initial/redir#1] (2) [perdir /home/stefano/webroot_aliases/testsite/] rewrite 'home/stefano/webroot_aliases/
testsite/skylight2/articles/view/' -> 'skylight2/home/stefano/webroot_aliases/testsite/skylight2/articles/view//?site=testsite'
127.0.0.1 - - [09/Mar/2012:07:22:45 --0600] [testsite.webserver/sid#161fca0][rid#195c9e0/initial/redir#1] (3) split uri=skylight2/home/stefano/webroot_aliases/testsite/skylight2/articles/view//?sit
e=testsite -> uri=skylight2/home/stefano/webroot_aliases/testsite/skylight2/articles/view//, args=site=testsite&site=testsite
127.0.0.1 - - [09/Mar/2012:07:22:45 --0600] [testsite.webserver/sid#161fca0][rid#195c9e0/initial/redir#1] (3) [perdir /home/stefano/webroot_aliases/testsite/] add per-dir prefix: skylight2/home/ste
fano/webroot_aliases/testsite/skylight2/articles/view// -> /home/stefano/webroot_aliases/testsite/skylight2/home/stefano/webroot_aliases/testsite/skylight2/articles/view//
127.0.0.1 - - [09/Mar/2012:07:22:45 --0600] [testsite.webserver/sid#161fca0][rid#195c9e0/initial/redir#1] (1) [perdir /home/stefano/webroot_aliases/testsite/] internal redirect with /home/stefano/w
ebroot_aliases/testsite/skylight2/home/stefano/webroot_aliases/testsite/skylight2/articles/view// [INTERNAL REDIRECT]
And so on in an infinite loop.
Why does apache keep adding the path info? Neither L nor DPI flags work.
EDIT #2
Following TerryE's advice I used this config:
RewriteEngine On
RewriteBase /testsite
RewriteRule ^(?!skylight2)(.*)$ /skylight2/$1?site=testsite [QSA,L]
And got this:
127.0.0.1 - - [10/Mar/2012:08:16:58 --0600] [testsite.webserver/sid#1575ca0][rid#2d6a740/initial] (3) [perdir /home/stefano/webroot_aliases/testsite/] add path info postfix: /home/stefano/webroot_aliases/testsite/articles -> /home/stefano/webroot_aliases/testsite/articles/view
127.0.0.1 - - [10/Mar/2012:08:16:58 --0600] [testsite.webserver/sid#1575ca0][rid#2d6a740/initial] (3) [perdir /home/stefano/webroot_aliases/testsite/] strip per-dir prefix: /home/stefano/webroot_aliases/testsite/articles/view -> articles/view
127.0.0.1 - - [10/Mar/2012:08:16:58 --0600] [testsite.webserver/sid#1575ca0][rid#2d6a740/initial] (3) [perdir /home/stefano/webroot_aliases/testsite/] applying pattern '^(?!skylight2)(.*)$' to uri 'articles/view'
127.0.0.1 - - [10/Mar/2012:08:16:58 --0600] [testsite.webserver/sid#1575ca0][rid#2d6a740/initial] (2) [perdir /home/stefano/webroot_aliases/testsite/] rewrite 'articles/view' -> '/skylight2/articles/view?site=testsite'
127.0.0.1 - - [10/Mar/2012:08:16:58 --0600] [testsite.webserver/sid#1575ca0][rid#2d6a740/initial] (3) split uri=/skylight2/articles/view?site=testsite -> uri=/skylight2/articles/view, args=site=testsite
127.0.0.1 - - [10/Mar/2012:08:16:58 --0600] [testsite.webserver/sid#1575ca0][rid#2d6a740/initial] (2) [perdir /home/stefano/webroot_aliases/testsite/] trying to replace prefix /home/stefano/webroot_aliases/testsite/ with /testsite
127.0.0.1 - - [10/Mar/2012:08:16:58 --0600] [testsite.webserver/sid#1575ca0][rid#2d6a740/initial] (1) [perdir /home/stefano/webroot_aliases/testsite/] internal redirect with /skylight2/articles/view [INTERNAL REDIRECT]
127.0.0.1 - - [10/Mar/2012:08:16:58 --0600] [testsite.webserver/sid#1575ca0][rid#18a9968/initial/redir#1] (3) [perdir /home/stefano/webroot_aliases/testsite/] add path info postfix: /home/stefano/webroot_aliases/testsite/skylight2 -> /home/stefano/webroot_aliases/testsite/skylight2/articles/view
127.0.0.1 - - [10/Mar/2012:08:16:58 --0600] [testsite.webserver/sid#1575ca0][rid#18a9968/initial/redir#1] (3) [perdir /home/stefano/webroot_aliases/testsite/] strip per-dir prefix: /home/stefano/webroot_aliases/testsite/skylight2/articles/view -> skylight2/articles/view
127.0.0.1 - - [10/Mar/2012:08:16:58 --0600] [testsite.webserver/sid#1575ca0][rid#18a9968/initial/redir#1] (3) [perdir /home/stefano/webroot_aliases/testsite/] applying pattern '^(?!skylight2)(.*)$' to uri 'skylight2/articles/view'
127.0.0.1 - - [10/Mar/2012:08:16:58 --0600] [testsite.webserver/sid#1575ca0][rid#18a9968/initial/redir#1] (1) [perdir /home/stefano/webroot_aliases/testsite/] pass through /home/stefano/webroot_aliases/testsite/skylight2
127.0.0.1 - - [10/Mar/2012:08:16:58 --0600] [testsite.webserver/sid#1575ca0][rid#18b1290/initial] (3) [perdir /home/stefano/webroot_aliases/testsite/] strip per-dir prefix: /home/stefano/webroot_aliases/testsite/favicon.ico -> favicon.ico
127.0.0.1 - - [10/Mar/2012:08:16:58 --0600] [testsite.webserver/sid#1575ca0][rid#18b1290/initial] (3) [perdir /home/stefano/webroot_aliases/testsite/] applying pattern '^(?!skylight2)(.*)$' to uri 'favicon.ico'
127.0.0.1 - - [10/Mar/2012:08:16:58 --0600] [testsite.webserver/sid#1575ca0][rid#18b1290/initial] (2) [perdir /home/stefano/webroot_aliases/testsite/] rewrite 'favicon.ico' -> '/skylight2/favicon.ico?site=testsite'
127.0.0.1 - - [10/Mar/2012:08:16:58 --0600] [testsite.webserver/sid#1575ca0][rid#18b1290/initial] (3) split uri=/skylight2/favicon.ico?site=testsite -> uri=/skylight2/favicon.ico, args=site=testsite
127.0.0.1 - - [10/Mar/2012:08:16:58 --0600] [testsite.webserver/sid#1575ca0][rid#18b1290/initial] (2) [perdir /home/stefano/webroot_aliases/testsite/] trying to replace prefix /home/stefano/webroot_aliases/testsite/ with /testsite
127.0.0.1 - - [10/Mar/2012:08:16:58 --0600] [testsite.webserver/sid#1575ca0][rid#18b1290/initial] (1) [perdir /home/stefano/webroot_aliases/testsite/] internal redirect with /skylight2/favicon.ico [INTERNAL REDIRECT]
127.0.0.1 - - [10/Mar/2012:08:16:58 --0600] [testsite.webserver/sid#1575ca0][rid#18a96f0/initial/redir#1] (3) [perdir /home/stefano/webroot_aliases/testsite/] add path info postfix: /home/stefano/webroot_aliases/testsite/skylight2 -> /home/stefano/webroot_aliases/testsite/skylight2/favicon.ico
127.0.0.1 - - [10/Mar/2012:08:16:58 --0600] [testsite.webserver/sid#1575ca0][rid#18a96f0/initial/redir#1] (3) [perdir /home/stefano/webroot_aliases/testsite/] strip per-dir prefix: /home/stefano/webroot_aliases/testsite/skylight2/favicon.ico -> skylight2/favicon.ico
127.0.0.1 - - [10/Mar/2012:08:16:58 --0600] [testsite.webserver/sid#1575ca0][rid#18a96f0/initial/redir#1] (3) [perdir /home/stefano/webroot_aliases/testsite/] applying pattern '^(?!skylight2)(.*)$' to uri 'skylight2/favicon.ico'
127.0.0.1 - - [10/Mar/2012:08:16:58 --0600] [testsite.webserver/sid#1575ca0][rid#18a96f0/initial/redir#1] (1) [perdir /home/stefano/webroot_aliases/testsite/] pass through /home/stefano/webroot_aliases/testsite/skylight2
Apache throws a "File does not exist: /home/stefano/webroot_aliases/testsite/skylight2" error. The correct URL should be instead /home/stefano/webroot_aliases/skylight2/articles/view.
Could it be because the VirtualDocumentRoot is set to testsite and I can't get out of it once it's set?
By the way, I'm using Apache 2.2.22.
EDIT #3
I actually solved it by placing a symlink "skylight2" in testsite, pointing to "../skylight2". thanks for the help!!
I've come across this bug before and there was an old bugrep on it which fizzled out. It can occur if you do a fall-through on a Per Directory context. But since the rewrite engine loops over the htaccess files anyway, the [L] doesn't mean last it really means "restart loop".
Next your rewrite rule is wrong since your document root is home/stefano/webroot_aliases and rule targets are in URI space not file space so your target should just be skylight2/$1/?site=testsite.
Next the Negative lookahead assertion should not be inside the (.*) loop, so try:
RewriteRule ^(?!skylight2)(.*)$ skylight2/$1/?site=testsite [QSA,L]
If this doesn't work, then append the new rewrite.log extract. Thanks.
Update following comments
The rewrite log shows a DOCROOT of /home/stefano/webroot_aliases/testsite and redirects
articles/view -> testsite/skylight2/articles/view/?site=testsite
Which fails the negative lookahead and loops again, so this is a case when you do need the leading / on the replacement string:
RewriteBase /testsite
RewriteRule ^(?!skylight2)(.*)$ /skylight2/$1/?site=testsite [QSA,L]

htaccess re-applies a matched rule constantly and ends up in a loop

According to http://htaccess.madewithlove.be/ the following ruleset works perfectly:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^images/([^\?]+)$ /_lib/mask_images.php [L]
RewriteRule ^search/([^\?]+)/merchant/([^\?]+)/brand/([^\?]+)/page/([^\?]+)$ /search.php?keyword=$1&merchant=$2&brand=$3&page=$4 [L]
RewriteRule ^search/([^\?]+)/brand/([^\?]+)/page/([^\?]+)$ /search.php?keyword=$1&brand=$2&page=$3 [L]
RewriteRule ^search/([^\?]+)/merchant/([^\?]+)/page/([^\?]+)$ /search.php?keyword=$1&merchant=$2&page=$3 [L]
RewriteRule ^search/([^\?]+)/page/([^\?]+)$ /search.php?keyword=$1&page=$2 [L]
RewriteRule ^search/([^\?]+)/brand/([^\?]+)$ /search.php?keyword=$1&brand=$2 [L]
RewriteRule ^search/([^\?]+)/merchant/([^\?]+)$ /search.php?keyword=$1&merchant=$2 [L]
RewriteRule ^search/([^\?]+)/merchant/([^\?]+)/brand/([^\?]+)$ /search.php?keyword=$1&merchant=$2&brand=$3 [L]
RewriteRule ^search/([^\?]+)$ /search.php?keyword=$1 [L]
RewriteRule ([^\?]+)/productID/([^\?]+)$ /detail.php?keyword=$1&productID=$2 [L]
RewriteRule ([^\?]+)$ /detail.php?keyword=$1 [L]
The issue is that when the last 2 rules are applied and matched, Apache re-applies the matched rules (and correctly assigned arguments) and goes into an infinite loop.
I added:
RewriteCond %{REQUEST_URI} !search.php$
RewriteCond %{REQUEST_URI} !detail.php$
RewriteCond %{REQUEST_URI} !index.php$
to the above ruleset. Again, verified and working by the htaccess tester website, it now stops processing and outputs "detail.php" as the argument.
Here is what the Rewrite Log says:
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaabfcc50/initial] (3) [perdir /var/www/html/] strip per-dir prefix: /var/www/html/hello -> hello
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaabfcc50/initial] (3) [perdir /var/www/html/] applying pattern '^images/(.*)$' to uri 'hello'
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaabfcc50/initial] (3) [perdir /var/www/html/] strip per-dir prefix: /var/www/html/hello -> hello
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaabfcc50/initial] (3) [perdir /var/www/html/] applying pattern '^search/(.*)/merchant/(.*)/brand/(.*)/page/(.*)$' to uri 'hello'
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaabfcc50/initial] (3) [perdir /var/www/html/] strip per-dir prefix: /var/www/html/hello -> hello
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaabfcc50/initial] (3) [perdir /var/www/html/] applying pattern '^search/(.*)/brand/(.*)/page/(.*)$' to uri 'hello'
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaabfcc50/initial] (3) [perdir /var/www/html/] strip per-dir prefix: /var/www/html/hello -> hello
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaabfcc50/initial] (3) [perdir /var/www/html/] applying pattern '^search/(.*)/merchant/(.*)/page/(.*)$' to uri 'hello'
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaabfcc50/initial] (3) [perdir /var/www/html/] strip per-dir prefix: /var/www/html/hello -> hello
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaabfcc50/initial] (3) [perdir /var/www/html/] applying pattern '^search/(.*)/page/(.*)$' to uri 'hello'
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaabfcc50/initial] (3) [perdir /var/www/html/] strip per-dir prefix: /var/www/html/hello -> hello
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaabfcc50/initial] (3) [perdir /var/www/html/] applying pattern '^search/(.*)/brand/(.*)$' to uri 'hello'
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaabfcc50/initial] (3) [perdir /var/www/html/] strip per-dir prefix: /var/www/html/hello -> hello
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaabfcc50/initial] (3) [perdir /var/www/html/] applying pattern '^search/(.*)/merchant/(.*)$' to uri 'hello'
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaabfcc50/initial] (3) [perdir /var/www/html/] strip per-dir prefix: /var/www/html/hello -> hello
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaabfcc50/initial] (3) [perdir /var/www/html/] applying pattern '^search/(.*)/merchant/(.*)/brand/(.*)$' to uri 'hello'
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaabfcc50/initial] (3) [perdir /var/www/html/] strip per-dir prefix: /var/www/html/hello -> hello
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaabfcc50/initial] (3) [perdir /var/www/html/] applying pattern '^search/(.*)$' to uri 'hello'
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaabfcc50/initial] (3) [perdir /var/www/html/] strip per-dir prefix: /var/www/html/hello -> hello
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaabfcc50/initial] (3) [perdir /var/www/html/] applying pattern '(.*)/productID/(.*)$' to uri 'hello'
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaabfcc50/initial] (3) [perdir /var/www/html/] strip per-dir prefix: /var/www/html/hello -> hello
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaabfcc50/initial] (3) [perdir /var/www/html/] applying pattern '(.*)$' to uri 'hello'
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaabfcc50/initial] (2) [perdir /var/www/html/] rewrite 'hello' -> '/detail.php?keyword=hello'
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaabfcc50/initial] (3) split uri=/detail.php?keyword=hello -> uri=/detail.php, args=keyword=hello
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaabfcc50/initial] (2) [perdir /var/www/html/] trying to replace prefix /var/www/html/ with /
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaabfcc50/initial] (1) [perdir /var/www/html/] internal redirect with /detail.php [INTERNAL REDIRECT]
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaac0e0c0/initial/redir#1] (3) [perdir /var/www/html/] strip per-dir prefix: /var/www/html/detail.php -> detail.php
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaac0e0c0/initial/redir#1] (3) [perdir /var/www/html/] applying pattern '^images/(.*)$' to uri 'detail.php'
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaac0e0c0/initial/redir#1] (3) [perdir /var/www/html/] strip per-dir prefix: /var/www/html/detail.php -> detail.php
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaac0e0c0/initial/redir#1] (3) [perdir /var/www/html/] applying pattern '^search/(.*)/merchant/(.*)/brand/(.*)/page/(.*)$' to uri 'detail.php'
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaac0e0c0/initial/redir#1] (3) [perdir /var/www/html/] strip per-dir prefix: /var/www/html/detail.php -> detail.php
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaac0e0c0/initial/redir#1] (3) [perdir /var/www/html/] applying pattern '^search/(.*)/brand/(.*)/page/(.*)$' to uri 'detail.php'
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaac0e0c0/initial/redir#1] (3) [perdir /var/www/html/] strip per-dir prefix: /var/www/html/detail.php -> detail.php
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaac0e0c0/initial/redir#1] (3) [perdir /var/www/html/] applying pattern '^search/(.*)/merchant/(.*)/page/(.*)$' to uri 'detail.php'
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaac0e0c0/initial/redir#1] (3) [perdir /var/www/html/] strip per-dir prefix: /var/www/html/detail.php -> detail.php
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaac0e0c0/initial/redir#1] (3) [perdir /var/www/html/] applying pattern '^search/(.*)/page/(.*)$' to uri 'detail.php'
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaac0e0c0/initial/redir#1] (3) [perdir /var/www/html/] strip per-dir prefix: /var/www/html/detail.php -> detail.php
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaac0e0c0/initial/redir#1] (3) [perdir /var/www/html/] applying pattern '^search/(.*)/brand/(.*)$' to uri 'detail.php'
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaac0e0c0/initial/redir#1] (3) [perdir /var/www/html/] strip per-dir prefix: /var/www/html/detail.php -> detail.php
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaac0e0c0/initial/redir#1] (3) [perdir /var/www/html/] applying pattern '^search/(.*)/merchant/(.*)$' to uri 'detail.php'
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaac0e0c0/initial/redir#1] (3) [perdir /var/www/html/] strip per-dir prefix: /var/www/html/detail.php -> detail.php
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaac0e0c0/initial/redir#1] (3) [perdir /var/www/html/] applying pattern '^search/(.*)/merchant/(.*)/brand/(.*)$' to uri 'detail.php'
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaac0e0c0/initial/redir#1] (3) [perdir /var/www/html/] strip per-dir prefix: /var/www/html/detail.php -> detail.php
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaac0e0c0/initial/redir#1] (3) [perdir /var/www/html/] applying pattern '^search/(.*)$' to uri 'detail.php'
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaac0e0c0/initial/redir#1] (3) [perdir /var/www/html/] strip per-dir prefix: /var/www/html/detail.php -> detail.php
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaac0e0c0/initial/redir#1] (3) [perdir /var/www/html/] applying pattern '(.*)/productID/(.*)$' to uri 'detail.php'
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaac0e0c0/initial/redir#1] (3) [perdir /var/www/html/] strip per-dir prefix: /var/www/html/detail.php -> detail.php
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaac0e0c0/initial/redir#1] (3) [perdir /var/www/html/] applying pattern '(.*)$' to uri 'detail.php'
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaac0e0c0/initial/redir#1] (2) [perdir /var/www/html/] rewrite 'detail.php' -> '/detail.php?keyword=detail.php'
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaac0e0c0/initial/redir#1] (3) split uri=/detail.php?keyword=detail.php -> uri=/detail.php, args=keyword=detail.php
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaac0e0c0/initial/redir#1] (2) [perdir /var/www/html/] trying to replace prefix /var/www/html/ with /
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaac0e0c0/initial/redir#1] (1) [perdir /var/www/html/] internal redirect with /detail.php [INTERNAL REDIRECT]
As you can see, it matches the correct rule and the arguments are setup correctly, but for some reason it then RE-APPLIES that internal redirect again and the argument going through changes from what it should be (which, in this example is "hello") to the file name, detail.php
NO idea how to go about this since everything I know of currently, this should be working because /search works brilliantly.
EDIT
This is what detail.php contains:
<?
echo $_GET["keyword"]."<br>";
echo $_GET["merchant"]."<br>";
echo $_GET["brand"]."<br>";
echo $_GET["page"]."<br>";
echo $_GET["productID"]."<br>";
exit();
?>
The final [L] to /detail.php does an (internal) redirect. And hence Apache sees a totally new request coming in - re-applying the rules ones more.
So options may be to make sure that the rule does not apply to the /detail.php (i.e. by ensuring the argument name is a bit different) or pre-cathcing an ^/detail.php early and negating out.
http://www.sitepoint.com/mod_rewrite-no-endless-loops/ tries to explain this with different words. Another common approach is using things like:
# Loop stopping snippets.
# Uncomment if there are specific pages to exempt.
# RewriteCond %{REQUEST_URI} ^/(stats/|missing\.html|failed_auth\.html|error/).* [NC]
# RewriteRule .* - [L]
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule .* - [L]
where you detect this on the second pass (after the internal redirect) and have it bail in a generic way early.
Dw.
Your indentation does not reflect the semantic association of the RewriteRule and RewriteCond directives.
The conditions
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
only apply if the URI pattern is ^images/([^\?]+)$. What you need is logically flip this and or at least skip the 10 following rules for these to apply for all URI patterns:
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule . - [SKIP=10]
See my article More on using Rewrite rules in .htaccess files for more details.