Forum Moderators: phranque

Message Too Old, No Replies

Subdomains created "on the fly"?

Other people can do it, how can I?

         

DMCunningham

1:31 pm on Jun 20, 2005 (gmt 0)

10+ Year Member



Hello,

I have been pulling my hair out trying to get something that seems very simple to work. What I would like to have happen is the following:

I would like to have the ability to create subdomains “on the fly” simply by creating a new directory in the “DocumentRoot” in Apache 2.

A great webhosting company, has such a feature for their customers, so I know it can be done but I just don't know how they do it.

The way it works is like this, say for example your DocumentRoot is:

/home/example.com/public_html

if you would like to create a subdomain like “hello.example.com” you would create a directory under “public_html” like this:

/home/example.com/public_html/hello

and you would place all files that you want served when someone visits hello.example.com in the “hello” directory.

In order to have “www.example.com” you must create the directory “www” that is under the “public_html” directory like this:

/home/example.com/public_html/www

So to summarize what I have been trying to achieve on my own with no luck is:

/home/example.com/public_html/www serves pages for www.example.com
/home/example.com/public_html/hello serves pages for hello.example.com
/home/example.com/public_html/today serves pages for today.example.com

A subdomain is created “on the fly” simply by creating a directory under the specified DocumentRoot for that Virtual Host.

Now, this is what I have tried:

My DNS record looks like this:


$ttl 38400
@ IN SOA example.org. server.example.org. (
2004112700
10800
3600
604800
38400 )
example.com. IN NS ns01.example.org.
example.com. IN NS ns02.example.org.
example.com. IN NS ns03.example.org.
@ IN A 123.123.123.123
ftp IN A 123.123.123.123
mail IN A 123.123.123.123
*.example.com. IN CNAME example.com.
example.com. IN MX 0 mail

My Virtual Host record is setup like this:


<VirtualHost *:80>
ServerName example.com
ServerAlias *.example.com
DocumentRoot /home/example.com/public_html
ErrorLog /home/example.com/logs/error.log
CustomLog /home/example.com/logs/access.log combined
ScriptAlias /cgi-bin/ /home/example.com/cgi-bin/

RewriteLog /home/example.com/logs/rewrite.log
RewriteLogLevel 9

<Directory /home/example.com/public_html>
Options Indexes IncludesNOEXEC FollowSymLinks -MultiViews
allow from all
RewriteEngine on
RewriteCond %{HTTP_HOST} .
#RewriteCond %{HTTP_HOST}!^www\.example\.com [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com [NC]
RewriteRule ^/(.*) /%1/$1 [L]
</Directory>
</VirtualHost>

Now, as you can see, I have tried to do what I want with the rewrite mod for Apache (the rewrite mod was enabled) and so far the only thing that happens when I create any directory under “public_html” like “ihopethisworks” is I get an index listing of “public_html”.

ihopethisworks.example.com

gives me an index listing of “public_html” instead of showing me the “index.html” file found in

/home/example.com/public_html/ihopethisworks

any no matter what directory I create and no matter what subdirectory I try I always get the index listing of everything that is in “public_html” instead of being served from the desired directories.

So far I have learned that the wildcard in my DNS record and in the Virtual Host configuration seems to work. I have also confirmed that I have no idea what I'm doing when it comes to working with mod_rewrite. :o

I have tried all kinds of mod_rewrite “tips” and “tricks” (thanks to searching Google and this web site) and I have come up with nothing.

The rewrite stuff that you see in the listing above is just one attempt and many different variations of rewrite “code.” I have placed that code in the <Directory> area, outside of it, right above it, below it, etc. I have tried to break the darn thing just so I can get some kind of error to at least know what was breaking so that I would have something to go from---but no dice.

The one thing that I have not tried is to place the rewrite “code” in an .htaccess file actually in the DoctumentRoot, as I want (if possible) it to run from the Apache configuration file. If in the long run I have to run the rewrite “code” from an .htaccess file in the DocumentRoot I guess I will have to do that then.

My RewriteLog never has anything in it. It remains exactly 0b and truly is empty.

So, because of the RewriteLog being empty, I don't know if what I'm doing is very, very wrong---so much so that any rewrite trick I try doesn't even get a chance to work to even record anything in the RewriteLog---or if the rewrite mod is not working correctly.

When I looked into

/etc/apache2/mods-enabled

I did see a link in their for rewrite that pointed to

/etc/apache2/mods-available

I enabled it by typing:


# [b][i]a2enmod[/i][/b]
Which module would you like to enable?
Your choices are: actions asis auth_anon auth_dbm auth_digest auth_ldap cache cern_meta cgid cgi dav_fs dav deflate disk_cache expires ext_filter file_cache headers imap include info ldap mem_cache mime_magic proxy_connect proxy_ftp proxy_http proxy rewrite speling ssl suexec unique_id userdir usertrack vhost_alias
Module name? [b][i]rewrite[/i][/b]
Module vhost_alias installed; run /etc/init.d/apache2 force-reload to enable.
# [b][i]/etc/init.d/apache2 force-reload[/b][/i]
Forcing reload of web server: Apache2.

And just to make things a little bit more clear (if this is needed) I am running Debian Linux (3.1) and everything (Apache2, PHP, MySQL,etc) was all installed via apt using the available Debian packages, so nothing was compiled nor built.

I don't even know if mod_rewrite is what I need for what I'm trying to do, but it seemed like that was the best solution for what I am going after. I just wish that the nice folks at <snip> would share with me their secret of how they do it.

Any tips, suggestions, and criticism is very welcomed.

Thank you in advance.

Sincerely,
David Michael Cunningham

[edited by: Woz at 1:48 pm (utc) on June 20, 2005]
[edit reason] No URLs please, see Tos#13 [/edit]

moltar

2:10 pm on Jun 20, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Have a look at mod_vhost_alias [httpd.apache.org]

DMCunningham

2:59 pm on Jun 20, 2005 (gmt 0)

10+ Year Member



OK, an update . . .

Apache2 and mod_rewrite are configured correctly (it was me that was making the error when I had set up Apache, but that was fixed). My RewriteLog is now logging and I am able to see something---I don't know what it means yet, but it gives me some clue as to what I should be looking for.

Now that I know that rewrite is working and I now know that my posix version is 1.51.2.3, I am going to look into what is happening in the RewriteLog and try some of the suggestions found on this board.

About the vhost_alias suggestion, at first glance it does not look to be what I need, however I will look further into it. Thank you very much for the suggestion.

moltar

3:35 pm on Jun 20, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



About the vhost_alias suggestion, at first glance it does not look to be what I need

Look again, it's exactly what you need. I have it setup exactly the way you wanted. It's probably faster than mod_rewrite too, because it has less features. And it sets appropriate ENV variables for you.

DMCunningham

5:27 pm on Jun 20, 2005 (gmt 0)

10+ Year Member



Thank you for the suggestion of mod_vhost_alias.

After only 3 min. of playing with it I quickly began so see that this may be for me after all.

There are a few things that are tripping me up right now, and with time, experimentation, and reading some more, I sense that I may be able to get this to work for me.

I'll post back with my findings. Again, thank you very much.

moltar

5:53 pm on Jun 20, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I had a recent post on this subject. It's not exactly what you are looking for, but I think it would help you.

VirtualHost config tip [webmasterworld.com]

DMCunningham

6:12 pm on Jun 20, 2005 (gmt 0)

10+ Year Member



This is what I have now in my configuration

ServerName example.com 
ServerAlias *.example.com

VirtualDocumentRoot /home/example.com/public_html/%1

ErrorLog /home/example.com/logs/error.log
CustomLog /home/example.com/logs/access.log combined
ScriptAlias /cgi-bin/ /home/example.com/cgi-bin/

<Directory /home/example.com/public_html/*>
Options Indexes IncludesNOEXEC FollowSymLinks -MultiViews
allow from all
</Directory>

Everything works great---just the way that I had wanted---except for one little thing.

The way that I have it set up, the virtual root creates the root by grabbing the first part of the URL, which works great if someone types in

something.example.com

because it pulls the files found under

/home/example.com/public_html/something

However when someone just types in

example.com

you get an error since there isn't a directory

/home/example.com/public_html/example

Is there a way to have the server serve the files found under

/home/example.com/public_html/www (which is what it would serve when you type www.example.com)

when typing in only example.com?

If I can figure out a solution to that, I sense that all of my problems will have been solved.

Thank you so very much for all of your help and time.

DMCunningham

6:18 pm on Jun 20, 2005 (gmt 0)

10+ Year Member



I should also state that I would not like to have to use symbolic links. I would like to be able to do this from the config file (If I am able to).

moltar

6:26 pm on Jun 20, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



For that case you could use mod_rewrite to forward one to another. It's good from SEO point of view too. It's better to have only one address, because it people will start linking to www.example.com and just example.com, then SEs get confused sometimes.

I use the following code to redirect non-www to www:


RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule (.*) [%{HTTP_HOST}...] [R=301,L]

DMCunningham

6:42 pm on Jun 20, 2005 (gmt 0)

10+ Year Member



RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule (.*) [%{HTTP_HOST}...] [R=301,L]

Well, it kind of worked . . .

when I type in

http://example.com

I get

http://www.example.com//

Yes, the 2 slashes do show up at the end of the url.

when I type

http://example.com/test.php

I get

http://www.example.com//test.php

I am very thankful that you have helped me this far with this. Thank you so much.

moltar

6:44 pm on Jun 20, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try:

... [%{HTTP_HOST}$1...]  ...

Notice there is no slash in between the two variables.

DMCunningham

6:53 pm on Jun 20, 2005 (gmt 0)

10+ Year Member



I tried that right after I posted my last msg. and it worked perfect. I am happy to see that my experimenting and your last suggestion was the same.

I have to go out for a while now. When I return I plan on posting a review of what you and I have worked through to make "Subdomains created 'on the fly'" work for me.

Thank you so very much for your time and effort into this interesting task.

DMCunningham

2:31 pm on Jun 21, 2005 (gmt 0)

10+ Year Member



Creating subdomains on the fly using Apache 2.x can be as simple as creating new directories.

Of course this is just a quick and dirty guide. So it would be in your best interest to read up on all of the topics discussed (Bind 9, Apache 2.x, virtual web hosting, mod_vhost_alias, mod_rewrite, etc.) to better understand what these things can do for you and how to customize them to you specific needs.

This “how to” was based around my needs and the servers that I have available to me. Your needs and interests may (and probably will) be different. With that said, please understand that this is not a “cut and paste” discussion. Think of it as a basis of a recipe (and how many of us follow all recipes to the letter?).

Let's say that you own the domain example.com and your DocumentRoot is

/home/example.com/public_html

If you wanted a subdomain called “test,” then you would just create the directory
/home/example.com/public_html/test

and upload the files in there that you wanted to see at [test.example.com...]

There are a few things that you must do to make this happen.

DNS Wildcards

First of all you need to setup a wildcard for DNS, so that anything.example.com will be sent to your web server's IP address. If you're using Bind 9 or later, you can do it by changing the zone file to look something like this:



$ttl 38400
@ IN SOA host.com. admin.host.com. (
2004112700
10800
3600
604800
38400 )
example.com. IN NS dns01.host.com.
example.com. IN NS dns02.host.com.
example.com. IN NS dns03.host.com.
example.com. IN A 111.222.333.444
ftp IN A 111.222.333.444
mail IN A 111.222.333.444
example.com. IN MX 0 mail
*.example.com. IN CNAME example.com.


Make sure you have all valid subdomains (ftp, mail, etc.) declared above the wildcard declaration (*.example.com.).

Setting up Apache

The next thing you need to do is configure Apache to answer for all subdomains requested (anything.example.com). Make sure you have enabled mod_vhost_alias and mod_rewrite in Apache 2.x. Since you are setting up a catchall wildcard domain, you will have to put it at the very end of all the other valid subdomains, like validsub.example.com below:



################
# This is a valid subdomain that does not change
# like the wildcard example below.
################
<VirtualHost *:80>
ServerName validsub.example.com
DocumentRoot /home/example.com/validsub/public_html
UseCanonicalName Off
IndexOptions FancyIndexing
</VirtualHost>

################
# Since this is the catchall wildcard domain,
# we have to put it at the very end of all the other
# valid subdomains, like validsub.example.com above
################
<VirtualHost *:80>
ServerName example.com
ServerAlias *.example.com #wildcard catch all
VirtualDocumentRoot /home/example.com/public_html/%1
UseCanonicalName Off
IndexOptions FancyIndexing
### Use mod_rewrite to direct example.com to www.example.com
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule (.*) http://www.%{HTTP_HOST}$1 [R=301,L]
</VirtualHost>


In the example above VirtualDocumentRoot was used instead of DocumentRoot. The “%1” at the end of the path tells the server to look at the first part of the URL (in our case the subdomain) and to use that value as the directory to serve that subdomain's web pages and files from.

Since all subdomains will be required to have a directory of their own, we need to set up a “www” directory to hold the files for www.example.com. Since the “%1” tells the server to look at the first part of the domain, we used mod_rewrite to send all “example.com” requests to “www.example.com.” If we did not do that, the server would then look for the path

/home/example.com/public_html/example

which does not exist (unless you wanted it to, and in that case you would not need to use mod_rewrite).

Keep in mind the above examples all use name based virtual web hosting. If you are using IP based virtual hosting, then you would want to change the “VirtualHost” tag as needed, change “UseCanonicalName Off” to “UseCanonicalName On”, and change “VirtualDocumentRoot” to “ VirtualDocumentRootIP.”

[edited by: Woz at 10:32 pm (utc) on June 21, 2005]
[edit reason] Formatting [/edit]