I am attempting to write some mod_rewrite rules that permit my customers to employ a single folder for doing development on different projects, without having to wreck havoc on adding vhosts for every project.

My idea to do this, is to setup a "Global VHost" for every user who needs this ability (only 3-4), the vhost could be something similar to: .my-domain.com. After that, I wish to promote my customers to create code as though it were on the domain, and never inside a sub folder. For instance, if bob was focusing on a task named 'gnome,' I would like the URL bob (and other people on our internal network) loads to get at this project to become: http://gnome.bob.my-domain.com. But, what I would like Apache to complete, is notice that "gnome" is really a "project" and therefore map the request, internally, to bob.my-domain.com/gnome/.

I have got things i thought works, and it is fairly simple, but..it does not work! The request just adopts an infinite loop and keeps prefixing the sub domain to the re-written request URI.

The mod rewrite code i've is:

RewriteEngine On

RewriteCond %{HTTP_HOST}	^([^.]+)\.bob\.my-domain\.com
RewriteCond %{REQUEST_URI}	!^/%1.*
RewriteRule ^(.*)$		/%1/$1 [L]

I have researched around a little relating to this, but I have yet to locate any real solutions that actually work. Has anybody attempted this - or possibly, does anybody possess a better idea? One which does not involve creating a virtual host for each project (I have got designers..I believe everybody would agree that the designer should not make virtual hosts..)

Thanks!

This is a snippet in the rewrite_log:

[rid#838dc88/initial] (3) [perdir /home/bob/http/] strip per-dir prefix: /home/bob/http/index.html -> index.html
[rid#838dc88/initial] (3) [perdir /home/bob/http/] applying pattern '^(.*)$' to uri 'index.html'
[rid#838dc88/initial] (4) [perdir /home/bob/http/] RewriteCond: input='gnome.bob.my-domain.com' pattern='^([^.]+)\.bob\.my-domain\.com' => matched
[rid#838dc88/initial] (4) [perdir /home/bob/http/] RewriteCond: input='/index.html' pattern='!^/%1.*' => matched
[rid#838dc88/initial] (2) [perdir /home/bob/http/] rewrite 'index.html' -> '/gnome/index.html'
[rid#838dc88/initial] (1) [perdir /home/bob/http/] internal redirect with /gnome/index.html [INTERNAL REDIRECT]
[rid#8392f30/initial/redir#1] (3) [perdir /home/bob/http/] strip per-dir prefix: /home/bob/http/gnome/index.html -> gnome/index.html
[rid#8392f30/initial/redir#1] (3) [perdir /home/bob/http/] applying pattern '^(.*)$' to uri 'gnome/index.html'
[rid#8392f30/initial/redir#1] (4) [perdir /home/bob/http/] RewriteCond: input='gnome.bob.my-domain.com' pattern='^([^\.]+)\.bob\.my-domain\.com' => matched
[rid#8392f30/initial/redir#1] (4) [perdir /home/bob/http/] RewriteCond: input='/gnome/index.html' pattern='!^/%1.*' => matched
[rid#8392f30/initial/redir#1] (2) [perdir /home/bob/http/] rewrite 'gnome/index.html' -> '/gnome/gnome/index.html'
[rid#8392f30/initial/redir#1] (1) [perdir /home/bob/http/] internal redirect with /gnome/gnome/index.html [INTERNAL REDIRECT]
[rid#8397970/initial/redir#2] (3) [perdir /home/bob/http/] add path info postfix: /home/bob/http/gnome/gnome -> /home/bob/http/gnome/gnome/index.html

Case a snippet, you will find a couple of 10s or 100 approximately lines of apache essentially spinning /gnome/index.html to /gnome/gnome/gnome/gnome/gnome/index.html, etc before apache hits its rewrite limit, surrenders, and throws error 500

Some Questions:

You stated "map internally" -- would you NOT desire to use a redirect?

Are you currently utilizing the same VirtualHost for gnome.bob.mysite.com and bob.mysite.com

Have you remember to produce a ServerAlias for *.bob.mysite.com?


This is a rough version you could modify to operate. It'll capture the subdomain and asked for URL, and perform a redirect towards the primary domain using the subdomain as part one from the path, then the asked for path, then the query string.

ServerName www.mysite.com
ServerAlias *.mysite.com

RewriteCond %{HTTP_HOST} ^([a-zA-Z0-9-]+)\\.mysite.com$
RewriteRule ^/(.*)       http://www.mysite.com/%1/$1        [R=301,L]',