Forum Moderators: phranque

Message Too Old, No Replies

RewriteRule - remove root context

         

arcing

9:44 am on Jan 22, 2009 (gmt 0)

10+ Year Member



Looking for some help with what I would have thought shouldn't be too hard to achive, but this is the first time I've had to do it and I'm getting a little lost.

I have a few sites we have are all under a single context root, so:

www.domain.com/root/site1
www.domain.com/root/site2
www.domain.com/root/site3

I want users to be able to hit www.domain.com/site1 and be forwarded. If this can be done without changing the URL in the browser then even better.

I've got as far as:

RewriteRule ^/(.*)$ /root/$1 [PT]

This redirects the first page fine, but if you hit any links in the web page they obviously still have the original context root, so this rule adds /root to it again, and the page cannot be found:

www.domain.com/root/site1/page1 becomes www.domain.com/root/root/site1/page1 and 404s.

I'm obviously missing something fundamental but mod-rewrite seems like a mine field.

Thanks!

g1smd

10:59 am on Jan 22, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The rewritten URL matches the rule and is rewritten again. That rewritten rule matches the rule and is rewritten again. Infinite loop.

That's because you used

.*
which matches "everything".

You need to make the pattern to be matched more exact and/or test

THE_REQUEST
in a
RewriteCond
to make sure that the requested URL was as a result of a client request, not as a result of a previous rewrite, or you could test that the URL does NOT contain "root".

jdMorgan

3:06 pm on Jan 22, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Just exclude requests for URL-paths starting with "root" from being rewritten:

RewriteCond $1 !^root/
RewriteRule ^/(.*)$ /root/$1 [PT,L]

Jim