homepage Welcome to WebmasterWorld Guest from 23.22.217.122
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
Rewrite Help
mbennie




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

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?

 

jdMorgan




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

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]

mbennie




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

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.

jdMorgan




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


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

mbennie




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

Thanks JD.

What does the 2 in the curly braces represent?

phranque




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

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.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved