Redirect subdomains to folders but show subdomain in address

Using a single web hosting account to host multiple sites

Redirect subdomains to folders but show subdomain in address

Postby Frustrated » Sun Apr 17, 2005 7:27 pm

Hi!

I'm setting up a membership site and I want each user to have their
own subdomain. For example:

http://subdomain.mysite.com which would point to:
http://www.mysite.com/user_dirs/subdomain/

When a member account is created, the subdomain is used in a
script that creates the folder /home/www/user_dirs/subdomain in the
server's filesystem.

I've had my host set up wildcard DNS so that I can accept
any request of the form *.mysite.com

I'm on a dedicated server and there's a <VirtualHost 1.2.3.4>
container in httpd.conf for each site. I can edit httpd.conf at will.

Since I expect to have potentially 1000s of subdomains, I want to
manage all this in httpd.conf and NOT use .htaccess files - for speed
and efficiency.

Further, I want the address bar to show:
http://billybob.mysite.com for any request other than:
http://www.mysite.com or http://mysite.com

If there's no match between the subdomain in the request and a
directory in the filesystem, I suppose it should simply go to:
http://www.mysite.com or else give an error (404?).

Can this be done? If so, how do I do it?

Thanks in advance!
Frustrated
 
Posts: 4
Joined: Sun Apr 17, 2005 6:51 pm

Git 'er done!

Postby Frustrated » Mon Apr 18, 2005 12:40 pm

Thanks in advance to anyone who was looking into this post. I've
found a perfect solution. No .htaccess files, no restarting Apache.
And the subdomains work with or without a "www." prefix.

Yee-Ha!

If anyone else has a similar problem, I'd be willing to share my solution.
Frustrated
 
Posts: 4
Joined: Sun Apr 17, 2005 6:51 pm

subdomain pointing

Postby ralucaoprean » Fri Apr 22, 2005 3:11 am

I have the same problem that you had, but I must edit a htaccess file because I don't have access to the http.conf file.
Can you share your solution to this problem?
Thanks in advance!

Raluca
ralucaoprean
 
Posts: 3
Joined: Fri Mar 25, 2005 2:55 am
Location: sibiu

Solution to subdomain redirection

Postby Frustrated » Fri Apr 22, 2005 6:36 am

Hi Raluca,

Here's what I did:

1) Had my hosting company set up "wildcard DNS". That makes their
nameserver accept anything.domain.com as a request for something
in the namespace of domain.com

2) Edited the httpd.conf file. You may be able to get your host to do
this for you since you can't do it yourself.

Here's what I put into the httpd.conf file:

<VirtualHost 11.22.33.44>
DocumentRoot "/home/owner/domain.com/htdocs"
ServerName www.domain.com
ScriptAlias /cgi-bin/ /home/owner/domain.com/cgi-bin/
LogLevel emerg
CustomLog /home/owner/domain.com/logs/access_log "combined"
ErrorLog /home/owner/domain.com/logs/error_log
ServerAlias *.domain.com
LimitRequestBody 15360000
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.domain\.com [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+)\.domain\.com$
RewriteCond ^www\.domain\.com/folder/%2 !-d
RewriteRule ^(.*)$ /folder/%2/$1 [L]
</VirtualHost>

Since your web site is most likely on a shared host, there's probably
a <VirtualHost 11.22.33.44> ... </VirtualHost> block in httpd.conf file.

In the first line is the IP address. There are probably several sites
on the same IP address, so the lines:

DocumentRoot "/home/owner/domain.com/htdocs"
ServerName www.domain.com
ScriptAlias /cgi-bin/ /home/owner/domain.com/cgi-bin/

These Unix/Linux paths may be different on your server!

tell the system where your /htdocs (or /www or /public_html) folder
is located in that computer's file system. The third of those 3 lines
helps it find CGI scripts for your site.

This line:

ServerAlias *.domain.com

means that anything.domain.com is a potentially valid address,
as well as that any path below it in the file system tree could point
to a real file.

This line:

Options +FollowSymLinks

might not matter but it can't really hurt.

This line:

RewriteEngine on

activates the rewiting function for this site even if no other site on
'the server is using it (in most cases... but YMMV)

Now for the magic part. I want ot make sure that any request for a
page, etc. in the main domain is ignored by the rewite engine, so
this line:

RewriteCond %{HTTP_HOST} !^www\.domain\.com [NC]

takes care of it. Only requests that DON't start with:
www.domain.com (not case sensitive) can be rewritten.

The next condition is that any request that starts with:

subdomain.domain.com (with or without "www.") can be rewritten.

This line does the trick:

RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+)\.domain\.com$

In the "regular expression", this part-> ([^\.]+) is the subdomain.
Because it's in parentheses, it will be "remembered" as %1 That will
important later in this block.

Next I wanted to figure out is a subdomain matched up with a real
directory on the server. So this line:

RewriteCond ^www\.domain\.com/folder/%2 !-d

says "If there's no such folder, ignore the request". The !-d is the
important thing here. It means "is not a directory".

Now for the rewrite rule itself:

RewriteRule ^(.*)$ /folder/%2/$1 [L]

It says "map anything that satified all the conditions to a file in
the subdirectory /folder or in any directory below it. So if a user
asks for: subdomain.domain.com/archives/item125.html, the server
will find it here: /folder/archives/item125.html

In my case, all files that "belong" to subdomains are below /folder.
If the possible paths to files "owned" by the subdomains included
stuff that was directly below the /htdocs (or /www or /public_html)
directories, the last two lines would look more like this:

RewriteCond ^www\.domain\.com/%1 !-d
RewriteRule ^(.*)$ /%1/$1 [L]

Try editing these lines to match your system and getting tech support
at your hosting company to add them to httpd.conf. If they won't do
it for you, then I guess you're stuck with using .htaccess files in
every directory that "belongs" to a subdomain. That's going to mean
being very careful about how you edit the regular expressions. And
it may or may not work at all.

kaspar
Frustrated
 
Posts: 4
Joined: Sun Apr 17, 2005 6:51 pm

Solution to subdomain redirection

Postby Frustrated » Fri Apr 22, 2005 7:03 am

Hi Raluca,

Here's what I did:

1) Had my hosting company set up "wildcard DNS". That makes their
nameserver accept anything.domain.com as a request for something
in the namespace of domain.com

2) Edited the httpd.conf file. You may be able to get your host to do
this for you since you can't do it yourself.

Here's what I put into the httpd.conf file:

<VirtualHost 11.22.33.44>
DocumentRoot "/home/owner/domain.com/htdocs"
ServerName www.domain.com
ScriptAlias /cgi-bin/ /home/owner/domain.com/cgi-bin/
LogLevel emerg
CustomLog /home/owner/domain.com/logs/access_log "combined"
ErrorLog /home/owner/domain.com/logs/error_log
ServerAlias *.domain.com
LimitRequestBody 15360000
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.domain\.com [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+)\.domain\.com$
RewriteCond ^www\.domain\.com/folder/%2 !-d
RewriteRule ^(.*)$ /folder/%2/$1 [L]
</VirtualHost>

Since your web site is most likely on a shared host, there's probably
a <VirtualHost 11.22.33.44> ... </VirtualHost> block in httpd.conf file.

In the first line is the IP address. There are probably several sites
on the same IP address, so the lines:

DocumentRoot "/home/owner/domain.com/htdocs"
ServerName www.domain.com
ScriptAlias /cgi-bin/ /home/owner/domain.com/cgi-bin/

These Unix/Linux paths may be different on your server!

tell the system where your /htdocs (or /www or /public_html) folder
is located in that computer's file system. The third of those 3 lines
helps it find CGI scripts for your site.

This line:

ServerAlias *.domain.com

means that anything.domain.com is a potentially valid address,
as well as that any path below it in the file system tree could point
to a real file.

This line:

Options +FollowSymLinks

might not matter but it can't really hurt.

This line:

RewriteEngine on

activates the rewiting function for this site even if no other site on
'the server is using it (in most cases... but YMMV)

Now for the magic part. I want ot make sure that any request for a
page, etc. in the main domain is ignored by the rewite engine, so
this line:

RewriteCond %{HTTP_HOST} !^www\.domain\.com [NC]

takes care of it. Only requests that DON't start with:
www.domain.com (not case sensitive) can be rewritten.

The next condition is that any request that starts with:

subdomain.domain.com (with or without "www.") can be rewritten.

This line does the trick:

RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+)\.domain\.com$

In the "regular expression", this part-> ([^\.]+) is the subdomain.
Because it's in parentheses, it will be "remembered" as %1 That will
important later in this block.

Next I wanted to figure out is a subdomain matched up with a real
directory on the server. So this line:

RewriteCond ^www\.domain\.com/folder/%2 !-d

says "If there's no such folder, ignore the request". The !-d is the
important thing here. It means "is not a directory".

Now for the rewrite rule itself:

RewriteRule ^(.*)$ /folder/%2/$1 [L]

It says "map anything that satified all the conditions to a file in
the subdirectory /folder or in any directory below it. So if a user
asks for: subdomain.domain.com/archives/item125.html, the server
will find it here: /folder/archives/item125.html

In my case, all files that "belong" to subdomains are below /folder.
If the possible paths to files "owned" by the subdomains included
stuff that was directly below the /htdocs (or /www or /public_html)
directories, the last two lines would look more like this:

RewriteCond ^www\.domain\.com/%1 !-d
RewriteRule ^(.*)$ /%1/$1 [L]

Try editing these lines to match your system and getting tech support
at your hosting company to add them to httpd.conf. If they won't do
it for you, then I guess you're stuck with using .htaccess files in
every directory that "belongs" to a subdomain. That's going to mean
being very careful about how you edit the regular expressions. And
it may or may not work at all.

kaspar
Frustrated
 
Posts: 4
Joined: Sun Apr 17, 2005 6:51 pm


Return to Domain Handling

Who is online

Users browsing this forum: No registered users and 92 guests

cron