Forum Moderators: phranque
I've used the following setup...
--------------------------------------
RewriteCond %{HTTP_HOST}__SPACE__!^$
RewriteCond %{HTTP_HOST}__SPACE__!^(www\.)?mydomain\.com$ [NC]
RewriteCond %{HTTP_HOST}<->%{REQUEST_URI} ^(www\.)?([^.]+).*<->/([^/]+) [NC]
RewriteCond %2<->%3__SPACE__!^(.*)<->\1$ [NC]
RewriteRule ^(.+) /%2/$1 [L]
--------------------------------------
This works fine, but I want to rewrite like this:
--------------------------------------
[subdomain.mydomain.com...] to [mydomain.com...]
--------------------------------------
I've tried changing...
RewriteRule ^(.+) /%2/$1 [L]
to
RewriteRule ^(.+) /folder/%2/$1 [L]
but it doesn't work other than a server error. Does anyone know how to modify the htaccess file to rewrite this?
So let me ask a question: Are all of the 'objects' located in "/folders" just subdirectories that belong to your various subdomains? In other words, is the purpose of your "/folders" directory simply to store the files for your subdomains, without any other 'functions' (e.g. scripts shared among any or all of them) residing there?
If so, then you can use this much simpler (and much more portable) solution:
# if requested subdomain is not "www"
RewriteCond %{HTTP_HOST} !^(www\.)?example\.com [NC]
# and we have not already rewritten this request to the subdomain's subfolder
RewriteCond %{THE_REQUEST} !/folder/.
# extract requested subdomain to %2
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.example\.com
# rewrite the request to the subdomain's subfolder
RewriteRule (.*) /folder/%2/$1 [L]
This new code uses the simpler approach of disabling the rewrite to /folders if we've already done a rewrite to /folders, thereby preventing recursion in a way that does not depend on the complex 'compare' function that uses POSIX atomic back-references. It's also a lot easier to read and comprehend... :)
The more-complicated previous solution is only needed for cases where arbitrarily-named subdomains' subfolders were located in the main domain's root directory and there was no simple way to look at the local URL-path and determine that a subdomain's subfolder was being requested. If you've already got all of your subdomains' files neatly organized into a "my_subdomains" subdirectory, then the simple solution can be used.
Jim