Forum Moderators: phranque

Message Too Old, No Replies

Subdomains, Document_Root and mod_rewrite

How to configure?

         

BlackDex

5:34 pm on Dec 7, 2004 (gmt 0)

10+ Year Member



Hello ppl,

I Have an question about subdomain handeling.
I Have an domain, for example 'mydomain.com'.
Now i have an main account where every requesti is send to the main document_root for example '/home/sites/site123/web'
I Can also create user accounts wich are located at '/home/sites/site123/users/username'.
When an user wants to create an website it has to put his files here '/home/sites/site123/users/username/web'.
To access the site atm the user has to type this url 'mydomain.com/~username'. I Want to have it work like the following: 'username.mydomain.com'. Also it should not change the URL in the address-bar back to 'mydomain.com/~username', whatever.
Also, if the $DOCUMENT_ROOT var could be change in HTACCESS that would be nice, becouse then PHP scripts will work the right way.

I Have looked all over the web, but can't find anything. A lot is helping in to the right direction, but then it breaks for some reason.

Does anyone has an idea how to start or has an example of this?

Thx in Advance.

jdMorgan

2:22 am on Dec 8, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This recent thread on how to rewrite arbitrary subdomains to subdirectories [webmasterworld.com] may help get you started (see message 6).

As I understand it, not all servers will support the POSIX 1003.2 local back-reference used in that code. If your server does not support it, you may need to hard-code the username/subdirectories into the loop-prevention. At this time, pure Unix servers won't support it. FreeBSD servers will support it. Not sure about others--feedback will be welcome.

You'll need to put your code into the /users subdirectory in order for this to work in the environment you describe.

Jim

BlackDex

6:43 pm on Dec 8, 2004 (gmt 0)

10+ Year Member



Thx.. i have tested it out. And it partialy works.

When i enter 'subdomain.mydomain.com' it works greate!
But when i enter 'subdomain.mydomain.com/subfolder', it changes the URL in the browser to 'www.mydomain.com/subdomain/subfolder'.

Is there a way to make it work in that way that it doesn't changes the URL?

jdMorgan

8:10 pm on Dec 8, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The code I cited does an internal rewrite and does not communicate with the browser, so I think you may have some other code that's interfering. Only RewriteRules with the forms

RewriteRule <something> http://<domain><something> [b][R][/b]
RewriteRule <something> http://<domain><something> [b][R=301][/b]
RewriteRule <something> http://<domain><something> [b][R=302][/b]

will issue a redirect to the browser, which is why it changes the URL in its address bar. When using an internal rewrite, the server simply changes the filepath associated with the requested URI, and serves that content instead.

Jim

BlackDex

9:18 pm on Dec 8, 2004 (gmt 0)

10+ Year Member



I Have detected what is the prob.
When i just enter 'subdomain.mydomain.com/sub' it will change the URL. BUT when i enter 'subdomain.mydomain.com/sub/index.html', it will not.

Is there an way to fix this?

Thx i advance

Here is the code i use:
---


RewriteEngine On
# Rewrite <subdomain>.example.com/<path> to example.com/<subdomain>/<path>
#
# Skip rewrite if no hostname or if subdomain is www
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST}!^www\. [NC]
# Extract (required) subdomain (%1), and first path element (%3), discard port number if present (%2)
RewriteCond %{HTTP_HOST}<>%{REQUEST_URI} ^([^.]+)\.example\.com(:80)?<>/([^/]*) [NC]
# Rewrite only when subdomain not equal to first path element (prevents mod_rewrite recursion)
RewriteCond %1<>%3!^(.*)<>\1$ [NC]
# Rewrite to /subdomain/path
RewriteRule ^(.*) /%1/$1 [L]

---

jdMorgan

10:25 pm on Dec 8, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm sorry, I think you're not understanding what I said... The code you posted will not do any external redirects under any circumstances. It only does server-internal stuff. It will never change the address bar.

Some other code in your .htaccess files or in your server configuration is interfering with this code, and is generating an external redirect that causes your browser to change the URL in the address bar. Internal rewrites don't do that.

It's very common to have problems on hosts that involve having to use cpanel. I'm not allowed to post what I think the "c" in cpanel stands for. It often creates seriously-broken code and stuffs it into your config files.

Also, what happens if you enter 'subdomain.mydomain.com/sub/'
(a URL should always end with a filename or a '/')

Jim

BlackDex

10:49 pm on Dec 8, 2004 (gmt 0)

10+ Year Member



Owke..
Well then is there a way to check if that %3 var has an trailing / or not. And if it has that it applys applys the first rule and if not it skips it with [S] (If possible)?

BlackDex

9:55 pm on Dec 9, 2004 (gmt 0)

10+ Year Member



Well i have it working a litle bit.

The code i now use is the following


#
# Start Subdomain Rewrite
RewriteEngine On
#
# Rewrite <subdomain>.example.com/<path> to example.com/<subdomain>/<path>
# Skip rewrite if no hostname or if subdomain is www
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\. [NC]
#
# Fix URI if there is no trailing / and it is an folder
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+[^/])$ /$1/
#
# Extract (required) subdomain (%1), and first path element (%3), discard port number if present (%2)
#
RewriteCond %{HTTP_HOST}<->%{REQUEST_URI} ^([^.]+)\.example\.nl(:80)?<->/([^/]*) [NC]
#
# Rewrite only when subdomain not equal to first path element (prevents mod_rewrite recursion)
RewriteCond %1<->%3 !^(.*)<->\1$ [NC]
#
# Rewrite to /subdomain/path - And make it the last RewriteRule.
RewriteRule ^(.*) /%1/$1 [L]
# End Subdomain Rewrite.
#

I Have add the rule under the 'Fix URI if there...' comment line.

The only thing that now happens is that it doesn't change it in the browser. I don't know if this is an bad thing, becouse then, everything is fine :).

[UPDATE]
Hmm i have tested it a litle further, and now the default site doesn't load any more.
So it is almost fixed.. Im trying stuff now. If you have any ideas please tell me :) Thx...

Thx for helping.

[edited by: jdMorgan at 3:42 am (utc) on Dec. 11, 2004]
[edit reason] Obscured specifics [/edit]

jdMorgan

5:40 pm on Dec 10, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> Well then is there a way to check if that %3 var has an trailing / or not. And if it has that it [always applies] the first rule and if not it skips it with [S] (If possible)?

The easiest way to do this is to use a separate rule at the beginning, and do not use the [L] flag on it. That way, the rules following the trailing-slash fix-up will be processed after the fix-up rule, whether it is invoked or not.

The other alternative is to [OR] the fix-up with the other RewriteConds in you first rule.

Jim

BlackDex

10:12 pm on Dec 10, 2004 (gmt 0)

10+ Year Member



Well i have fixed it i think :).
Now it should works as intended.
The only thing for some reason doesn't work.
Is if i want to move all the subdomains to an other subdir.
Example:
document_root is "/home/sites/site000/web".
And i want to move all the subdirs to "/home/sites/site000/web/(.subdomains¦_subdomains)".
And change the rewriterule code to this: "RewriteRule ^(.*) /.subdomains/%1/$1 [L,NS]" The page stops loading the website :(.

But now it just works perfectly :).
Would be nice if i can locate them all in one directory.

But thx for the help :). I Learned alot about .htaccess files now.


############################
RewriteEngine On

############################
#### FIX TRAILING SLASH ####
############################
# Skip rewrite if no hostname or if subdomain is www
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\. [NC]
# Extract (required) subdomain (%1), and first path element (%3), discard port number if present (%2)
RewriteCond %{HTTP_HOST}<->%{REQUEST_URI} ^([^.]+)\.example\.nl(:80)?<->/([^/]*) [NC]
# Rewrite only when subdomain not equal to first path element (prevents mod_rewrite recursion)
RewriteCond %1<->%3 !^(.*)<->\1$ [NC]
# Check if URI has an trailing slash
RewriteCond %{REQUEST_URI} !/$
# Check if it is an directory and then rewrite it.
RewriteCond %{DOCUMENT_ROOT}/%1%{REQUEST_URI} -d
RewriteRule .* http://%{HTTP_HOST}%{REQUEST_URI}/ [R=301,L]
############################

############################
# Start Subdomain Rewrite
# Rewrite <subdomain>.example.com/<path> to example.com/<subdomain>/<path>
# Skip rewrite if no hostname or if subdomain is www
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\. [NC]
# Extract (required) subdomain (%1), and first path element (%3), discard port number if present (%2)
RewriteCond %{HTTP_HOST}<->%{REQUEST_URI} ^([^.]+)\.example\.nl(:80)?<->/([^/]*) [NC]
# Rewrite only when subdomain not equal to first path element (prevents mod_rewrite recursion)
RewriteCond %1<->%3 !^(.*)<->\1$ [NC]
# Check if subdomain (dir/file) exists, else give an error.
RewriteCond %{DOCUMENT_ROOT}/%1%{REQUEST_URI} -d [OR]
RewriteCond %{DOCUMENT_ROOT}/%1%{REQUEST_URI} -f
# Rewrite to /subdomain/path - And make it the last RewriteRule.
RewriteRule ^(.*) /%1/$1 [L,NS]
# End Subdomain Rewrite.
############################

[edited by: jdMorgan at 3:43 am (utc) on Dec. 11, 2004]
[edit reason] Obscured specifics [/edit]