Forum Moderators: phranque
So if http://images.mydomain.com is entered,
it transparently uses http://mydomain.com/images
I have a code but not working..
RewriteEngine on
// If the host is just mydomain.com, do nothing more
rewritecond %{http_host} ^(www\.)?example\.com$ [nc]
RewriteRule ^.*$ - [L]
// Otherwise strip off everything before mydomain
// And add it to the start of the request
rewritecond %{http_host} ^(.*?)\.(www\.)?example\.com$ [nc]
RewriteRule ^.*$ http://example.com/%1%{REQUEST_URI} [L]
[edited by: jdMorgan at 4:21 pm (utc) on Feb. 27, 2005]
[edit reason] Removed specifics per TOS. [/edit]
Welcome to WebmasterWorld!
The main problem is that code in .htaccess is effectively recursive, because of the way that Apache resolves URL-paths. If you do a rewrite in .htaccess, the new URL is then passed back to httpd.conf, and then through .htaccess again, so that further access controls and rewrites can be applied.
So, this kind of rewrite code must contain logic to prevent an infinite redirection loop.
The easiest way to do this is to check either the sudomain names or the related subdirectory names, and suppress the rewrite if the subdomain has already been mapped to a subdirectory. This is usually done with a fixed list which, as you have noted, requires hand-editing each time you add a subdomain.
There are two solutions, one simple and one rather complex. The first is to map
<subdomain>.example.com/<resource> to example.com/<stop_key><subomain>/<resource>
where <stop_key> is a string that you do not use in any other directory on the site. Then you can simply suppress redirects if that word appears in the first subdirectory path:
RewriteCond $1 !stop_key
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com
RewriteRule (.*) /stop_key%1/$1 [L]
Jim