Forum Moderators: phranque

Message Too Old, No Replies

Dynamic Subdomains (not again i hear you cry!)

         

anticulture

1:28 pm on Mar 21, 2006 (gmt 0)

10+ Year Member



Hello this is a follow up of a post at [webmasterworld.com...]
... and at [webmasterworld.com...]

i'm trying to let users have their own subdomains and route user.domain1.tld to domain1.tld/~user/
also i'm tring to use the .htaccess file on many domains, but if it's easier i can create one for each domain,

then the second part tries to say that there is no directory domain1.tld/~user if the user goes there...how they'd find out their files were there i dont know but just incase!

also a wild card CNAME dns record (my hosting company said a CNAME record as opposed to an A record would be better) has been set up for each domain AND (i'm informed) the virtual host info has been updated accordingly.

Well i've tried to figure it out using apache docs and an ilovejackdaniel dot com cheat sheet
i think i've got it ... and then it returns a Internal Server Error code (500 i think)!

the code is as follows and is place in a .htaccess file on each of the domains' root directory, i did experiment with having the domains under a main domain but didn't get on very well with that either!


RewriteEngine On
RewriteCond %{REQUEST_URI}!^/~
RewriteCond %{HTTP_HOST}!^www\.(domain1\.tld¦domain2\.tld¦domain3\.tld)
RewriteCond %{HTTP_HOST} ^([a-z0-9_-]+)\.(domain1\.tld¦domain2\.tld¦domain3\.tld)
RewriteRule (.*) /~%1/$1 [L]


RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /~
RewriteRule .* - [R=404]

Please help...!

jdMorgan

3:27 pm on Mar 21, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> It returns a Internal Server Error code (500 i think)

What's your server error log say when you get a 500 error?

If you are using IE, go into to Internet Options, and turn off anything that says 'Show friendly'. IE hides information that might be very helpful to you as a Webmaster.

I assume you've seen the warnings posted here about the forum dropping spaces between "}" and "!" characters, and changing solid vertical pipes to borken pipes "¦".

Your code is fairly solid, and should work. I'd only make a couple of tweaks to it:


RewriteEngine On
RewriteCond %{REQUEST_URI} !^/usr_
RewriteCond %{HTTP_HOST} !^www\.(domain1\.tld¦domain2\.tld¦domain3\.tld)
RewriteCond %{HTTP_HOST} ^([a-z][a-z0-9_-]+[a-z0-9])\.(domain1\.tld¦domain2\.tld¦domain3\.tld)
RewriteRule (.*) /usr_%1/$1 [L]

The use of "usr_" instead of "~" is to avoid unintended interaction with mod_userdir (which might be a simpler way to do what you're trying to do anyway), but you asked about mod_rewrite. The change to the subdomain pattern is intended to require that subdomain names start with alpha characters and end with alphanumeric characters only -- to prevent people from making up names that cause URL/DNS/path problems. You are far better off starting with very tight restrictions.

As implied, get this first part of the code working flawlessly before worrying about preventing direct-entry of the subdomain's subdirectory path.

Jim

anticulture

4:26 pm on Mar 21, 2006 (gmt 0)

10+ Year Member



Thank you, the code is now working flawlessly, i'll admit although i knew about the ¦ to ¦ i didn't know the spaces in between } and! and now thanks to your complete rewriting... i mean extra tweeks, its working beautifully,

the last part about not letting users see their folders should be changed from


RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /~

to

RewriteCond %{THE_REQUEST} ^[a-z][a-z0-9_-]+[a-z0-9]{3,9}\ /usr_

correct?

however i noticed in the error logs it said:


RewriteRule: invalid HTTP response code for flag 'R'

so what should it be?
would
RewriteRule .* - R[=404]
work?

Thanks again for your help

jdMorgan

8:57 pm on Mar 21, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, you can't use R=404, but there is no need to. Simply redirect them back to the correct subdomain:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /usr_([a-z][a-z0-9_-]+[a-z0-9]}/
RewriteRule (.*) http://%1.example.com/$1 [R=301,L]

Note the the value of THE_REQUEST will look just like the request header logged in your access log:

GET /usr_able123/my_file.html HTTP/1.1

The "^[A-Z]{3,9}\ " matches GET, POST, PROPFIND, etc., then a literal space, then the requested local URL-path. That's all that is necessary, since the purpose of the RewriteCond is simply to make sure that the request being processed is the initial client request, and not the result of a previous rewrite.

Jim

anticulture

9:30 pm on Mar 21, 2006 (gmt 0)

10+ Year Member



Hello Thanks for your replies you've bee a great help
i got it working although i need to point out that
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /usr_([a-z][a-z0-9_-]+[a-z0-9]}/
should be
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /usr_([a-z][a-z0-9_-]+[a-z0-9])/
notice the
)
instead of the
}

the only problem i have is that

RewriteRule (.*) http://%1.domain.com/$1 [R=301,L]

comes back with
http://abc-123.domain.com/usr_abc-123/
which i can't figure out!

Thank for your help though it's been invaluable!

jdMorgan

10:42 pm on Mar 21, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, got in a hurry, and missed both of those problems:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /usr_[a-z][a-z0-9_-]+[a-z0-9]/
RewriteRule ^usr_([a-z][a-z0-9_-]+[a-z0-9])/(.*) http://$1.example.com/$2 [R=301,L]

As above, although this code appears redundant, the purpose is to avoid internal-rewrite/external-redirect looping.
If you don't put *anything* other than 'user' files in the /usr_ subdirectory, then you can shorten that to:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /usr_
RewriteRule ^usr_([a-z][a-z0-9_-]+[a-z0-9])/(.*) http://$1.example.com/$2 [R=301,L]

Jim

anticulture

12:44 am on Mar 22, 2006 (gmt 0)

10+ Year Member



just got this,
thank you for all your help!
i shall be telling lots of people about how good ths site is!
Thanks again :)