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)?

link|improve this question
feedback

4 Answers

up vote 1 down vote accepted

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.

link|improve this answer
feedback

Try

RewriteCond C:/wamp/www%{REQUEST_FILENAME} !-f

or

RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-f

See also "What's up with REQUEST_FILENAME in mod_rewrite?"

link|improve this answer
feedback

I don't know where the "C:" is coming from in your browsers -- a more useful diagnostic would be to find the "Location" header from the HTTP response -- use something like Firebug, LiveHTTPHeaders, or telnet to get that.

Also, given that your second RewriteRule doesn't mention 'example.com' at all, I guess that you have both RewriteRules in your httpd.conf.

If that is the case, then the [L] modifier at the end of your first rule is stopping the second one from executing at all.

link|improve this answer
feedback

Why not just use this?

RewriteCond %{HTTP_HOST} ^(www\.)?example.com$  
RewriteCond %{REQUEST_URI} !^/example.com(.*)  
RewriteRule ^(.*) /example.com/$1  [L]
link|improve this answer
Not working, when I try www.example.com/test it give me this example.com/C:/wamp/www/example.com/test – Paul Sep 4 '09 at 2:45
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.