Tangentially:
RewriteCond %{HTTP_HOST} ^(www\.example\.com)?$
This is backward. Express the condition as
!^(example\.com)?$
where the part inside parentheses is identical to the form you use in the target of your rule.
So you've got two separate joomla installations along the same physical path (one directory inside another)? Does the inner one say anything about
RewriteOptions inherit
? Or does the domain-name redirect rule appear twice? I assume you want the same form of the name throughout the site.
If-and-only if the problem is happening within Apache alone, rather than in joomla, here is the likeliest explanation:
Since the second htaccess file is in a subdirectory, any pattern in the form
^blahblah
means
www.example.com/subdir/blahblah
This applies to any RewriteRule located in a subdirectory: ^ (opening anchor, or earliest possible part of pattern) means the
current directory. So if you want to capture and reuse from within this subdirectory, the target would have to say
www.example.com/subdir/$1