Forum Moderators: phranque
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.
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
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?
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]
Jim
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]
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
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]
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
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]