Mod_rewrite and trailing slash

apache-2.2mod-rewrite

I want to make

www.example.com/test/ rewrite to www.example.com/example.com/test/  
www.example.com/ rewrite to www.example.com/example.com/  

I can do that with this rewrite configuration:

RewriteCond %{HTTP_HOST} ^(www\.)?example.com$  
RewriteCond %{REQUEST_URI} !^/example.com(.*)  
RewriteCond %{REQUEST_URI} ^(.*)/$  
RewriteRule ^(.*) /example.com/$1  [L]  

But I also want to redirect the same without trailing slash:

www.example.com/test redirect to www.example.com/example.com/test/  
www.example.com/ redirect to www.example.com/example.com/  

I have tried with this:

RewriteCond %{HTTP_HOST} ^(www\.)?example.com$
RewriteCond %{REQUEST_URI} !^/example.com(.*)
RewriteCond %{REQUEST_URI} !^(.*)/$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*[^/])$ $1/ [R=301,L]

but does not work, it redirects to:

IE: http://www.example.com/C:/wamp/www/test/
Firefox: http://www.example.com/C:/wamp/www/example.com/test/

do you know a solution to this (without touching mod-dir conf)?

Best Answer

I've found the solution:

Options +FollowSymLinks
RewriteEngine On

# Fix missing trailing slashes.
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteCond %{DOCUMENT_ROOT}/_l_/example\.com%{REQUEST_URI}/ -d
RewriteRule [^/]$ %{REQUEST_URI}/ [R=301,L]

# Rewrite sub domains.
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ /_l_/example\.com/$1 [QSA,L]

Thanks for your answers.