Monday, December 19, 2011

Creating a Subdomain









Please Note: 

I have done tests on two different computers, and the results were the same. Please comment on any problems you may have. Becarful when adding comments to your directives, comma's and dots may cause issues with the directives.


 

One of the most difficult things to do when you are building a website, is to make a sub domain. A sub domain is written as, sub domain.domain.com.

You can think of it as a virtual domain. It is simply a server instruction that directs the user to a directory off of the domain you pay for, such as domain.com/example.

I just recently learned how to do this, and decided to take some time and document my findings. It wasn't easy. I must of spent half the day researching.

To create a sub domain follow these simple instructions;

1) Setup a sub directory.

2) Add a htaccess file to the root of the domain.

3) Configure the htaccess file to accept the following url, subdomain.domain.com


That's it folks. Its really that simple.

The only part that is not simple is configuring the htaccess file. Do it carefully, and it will work the first time.

The .htaccess file provides a way to make configuration changes on the server (on a per directory basis). A file that contains one or more directives is placed in the root directory, and the directives apply to that directory, and all sub directories bellow it.

If you have never used this file before, it can be quite daunting. The commands are not easy to learn, and you won't use it very much except for very specific tasks, like redirecting someone form one file to another. Its like being in command of the server, without being in total control.

Information on the .htacess file can be found at the Apache site. Search the Internet, and you will find hundreds of sites with the information you are seeking. Analysing each site is the hard part.

To start creating an htaccess file, save a file without an extension. The file name must be specified as .htaccess. The server will look for this file when someone visits your site with the modified url, subdomain.domain.com.

In the file add the following lines of code, and becareful;

Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} subdomain.domain.com
RewriteCond %{REQUEST_URI} !directory/
RewriteRule ^(.*)$ http://subdomain.domain.com/directory/$1 [L]


The server will not give you any idea of what's wrong. If you make a mistake, the server will just not do anything.

with these instructions you will be off and running. Creating a sub domain is difficult if you have never done anything like it before, but doesn't have to be. Bookmark this page, and forward this to all your friends.

Sub domains lend themselves to an infinite number of possibilities. You can use them for blogs, selling space off your hosting space (until you have too many customers, and have to spend money on a dedicated or static ip), and promoting products and services, without the cost of buying and maintaining another domain. Wow!

Programming for the Web