www.SmarteGuru.com
  Home | Blogs | Recipe | Find a Friend | Discussion Board | Resources | Developers Area | Articles | Health |  Login | Register Now 

How to create SubDomains by editing httpd.conf

If you want to maintain multiple domains/hostnames on your
machine you can setup VirtualHost containers for them.

At the end of my zone DB file (/var/named/example.com.db) added the following line:

*.example.com. 14400 IN A 215.51.182.53

That’s all you need, now restart bind. (For me /etc/init.d/named restart.)

Now you need to set up Apache to respond to requests on any hostname under example.com.
Before I just used the convinence of having a dedicated IP for this site and having the redirect
VirtualHost entry occur first in my httpd.conf file. That works, but I have a better solution now.
So we want to tell Apache to respond to any request on any subdomain
(that does not already have an existing subdomain entry) and redirect it to example.com.
Here’s what I have:

Change Apache’s httpd.conf file

NameVirtualHost 215.51.182.53
<VirtualHost 215.51.182.53>
DocumentRoot /home/example/public_html
BytesLog domlogs/example.com-bytes_log
ServerAlias *.example.com
ServerName www.example.com
CustomLog domlogs/example.com combined
RedirectMatch 301 (.*) http://example.com$1
</VirtualHost>

The two magic lines are the ServerAlias directive which is self explanitory and the RedirectMatch
line which redirects all requests to example.com in a permanent manner.

There is a catch though. The redirecting VirtualHost entry must come after any valid subdomain
VirtualHost entries you may have, for example I have one for test.example.com and I had to move
that entry up in the httpd.conf because Apache just moves down that file and uses the first one
it comes to that matches, so the wildcard should be last.

For wildcard DNS set up and first subdomain with mod_rewrite is working!
However second one using the following .htaccess file:

Options +FollowSymLinks
RewriteEngine On
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} ^(www\.)?blogs\.example\.com$ [NC]
RewriteRule ^$ /blogs/ [QSA,L]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} ^(www\.)?forums\.example\.com$ [NC]
RewriteRule ^$ /forums/ [QSA,L]

NOTE :
You can create n number of SubDoamins on one IP address

Related Posts

Tags: , , , , , , , , , , ,


Viewed: 182 views

Leave a Reply

Comment moderation is enabled. Your comment may take some time to appear.

Home - About Us - Help - Terms and Conditions - Site Map - Link to Us - Resources - Contact Us
Google Rank Calculator | Suggest developer resource | Suggest Article
All rights reserved © 2007 SmarteGuru.com.