Forum Moderators: phranque

Message Too Old, No Replies

htaccess mod rewrite subdomain to folder/subdomain

Need help on how to redirect a subdomain to a subfolder

         

LucentMinds

12:15 am on Sep 1, 2007 (gmt 0)

10+ Year Member



I've read topic: [webmasterworld.com...]

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?

jdMorgan

12:34 am on Sep 1, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You will also need to change the two RewriteConds preceding the rule. Their purpose is to make sure that the rule is not recursive. Because you are inserting /folder/ into the REQUEST_URI, you must allow for that in the first of those two RewriteConds.

Jim

LucentMinds

5:55 am on Sep 1, 2007 (gmt 0)

10+ Year Member



How would I change the expressions to make it work? I'm not picking up on what I'm seeing as fast as I would like.

jdMorgan

2:23 pm on Sep 1, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I've played with this for awhile, and the solution is overly-complicated, mostly because the original code was designed to solve a problem that you likely don't have.

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]

The main point of this code --and the previous more-complex code-- is to prevent recursive rewriting. So the example you worked from uses a POSIX atomic back-reference trick to compare the requested subdomain with the current subdirectory, and if they are the same (because we've already done the rewrite), it prevents the rewrite rule from executing a second time.

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