Forum Moderators: phranque
I am using a wildcard dns entry and the following htaccess code to route the traffic:
RewriteCond %{ENV:REDIRECT_SUBDOMAIN} =""
RewriteCond %{HTTP_HOST} ^([a-z0-9][-a-z0-9]+)\.example\.com\.?(:80)?$ [NC]
RewriteCond %{DOCUMENT_ROOT}/subdomains/%1 -d
RewriteRule ^(.*) subdomains/%1/$1 [E=SUBDOMAIN:%1,L]
RewriteRule ^ - [E=SUBDOMAIN:%{ENV:REDIRECT_SUBDOMAIN},L]
Apache has a hard limit of 32K subfolders which I am about to reach. I intend to reorganize the file system so that the user sites are in subdomains/[1st 3 letters of user name] folder.
Not being a mod-rewrite expert could anyone show me the correct method in htaccess to accomplish this?
Also, is there a limit on the maximum number of symlinks apache can handle?
First you're allowing subdomains to start with a number, which is not strictly valid per HTTP specifications.
Second, you're saying you want to sort the accounts into subdirectories based on the first two letters, but you're allowing the first two characters to be letters or numbers, and also allowing the second character to be a hyphen. You're also allowing uppercase and lowercase letters in all cases. This results in a total of 3906 subdirectories of /subdomains, with uppercase and lowercase variations being treated as separate username/accounts.
No single "perfect" solution will be possible until the above inconsistencies are resolved; You'll need to research your current user accounts and see what your actual requirements are.
To support your new requirement while bringing the code into HTTP compliance by requiring the subdomain to start with a letter followed by a letter, number, or hyphen, and allowing letters or numbers only as the last character, you could use:
RewriteCond %{ENV:REDIRECT_SUBDOMAIN} =""
RewriteCond %{HTTP_HOST} ^(([a-z][-a-z0-9])[-a-z0-9]*[a-z0-9])\.example\.com\.?(:80)?$ [NC]
RewriteCond %{DOCUMENT_ROOT}/subdomains/%2/%1 -d
RewriteRule (.*) subdomains/%2/%1/$1 [E=SUBDOMAIN:%1,L]
RewriteRule ^ - [E=SUBDOMAIN:%{ENV:REDIRECT_SUBDOMAIN},L]
It's also not clear to me why you are using two user environment variables, and replacing the value of %{ENV:SUBDOMAIN} with that of %{ENV:REDIRECT_SUBDOMAIN} (which is undefined in this code) in the last rule under certain circumstances. In order to prevent the first rule from looping, either that rule should also check %{ENV:SUBDOMAIN} for blank, or both variables should have the same name; Since part of the code dealing with this issue is apparently missing and what's here isn't commented, I cannot tell what the best solution would be.
Jim
[edited by: jdMorgan at 6:24 pm (utc) on Nov. 24, 2007]
It works for the first 2 letters of the subdomain but not the first 3.
I tried changing it to:
RewriteCond %{ENV:REDIRECT_SUBDOMAIN} =""
RewriteCond %{HTTP_HOST} ^(([a-z][-a-z0-9][-a-z0-9])*[a-z0-9])\.example\.com\.?(:80)?$ [NC]
RewriteCond %{DOCUMENT_ROOT}/subdomains/%2/%1 -d
RewriteRule (.*) subdomains/%2/%1/$1 [E=SUBDOMAIN:%1,L]
RewriteRule ^ - [E=SUBDOMAIN:%{ENV:REDIRECT_SUBDOMAIN},L]
And got errors.
RewriteCond %{ENV:REDIRECT_SUBDOMAIN} =""
RewriteCond %{HTTP_HOST} ^(([a-z][-a-z0-9]{2})[-a-z0-9]*[a-z0-9])\.example\.com\.?(:80)?$ [NC]
RewriteCond %{DOCUMENT_ROOT}/subdomains/%2/%1 -d
RewriteRule (.*) subdomains/%2/%1/$1 [E=SUBDOMAIN:%1,L]
RewriteRule ^ - [E=SUBDOMAIN:%{ENV:REDIRECT_SUBDOMAIN},L]
Jim