Welcome to WebmasterWorld Guest from 54.211.136.250

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

Rewrite Help

   
5:50 pm on Nov 24, 2007 (gmt 0)

10+ Year Member



I have s site that uses subdomains for user's accounts. Currently I have the sites set up as user.example.com.

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?

6:19 pm on Nov 24, 2007 (gmt 0)

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member



You've got a potential problem here, in two ways:

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]

Note that a minimum of three characters in the subdomain will be required.

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]

8:17 pm on Nov 24, 2007 (gmt 0)

10+ Year Member



Thanks but it doesn't seem to work.

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.

9:26 pm on Nov 24, 2007 (gmt 0)

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member




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]

Note that a minimum of four characters in the subdomain will be required.

Jim

11:06 pm on Nov 24, 2007 (gmt 0)

10+ Year Member



Thanks JD.

What does the 2 in the curly braces represent?

6:51 am on Nov 25, 2007 (gmt 0)

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



the 2 curly braces means 2 of the previously specified pattern, so (([a-z][-a-z0-9]{2})[-a-z0-9]*[a-z0-9]) means an alphabetic character, followed by two dashes-or-alphanumerics, followed by zero or more dashes-or-alphanumerics, followed by an alphanumeric.