HostOnNet.com Forum Index HostOnNet.com
Private Label Reseller Plan
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Wildcard dns and sub domains

 
Post new topic   Reply to topic    HostOnNet.com Forum Index -> Dedicated Servers
View previous topic :: View next topic  
Author Message
admin
Site Admin


Joined: 23 Jan 2003
Posts: 256
Location: India

PostPosted: Wed Oct 26, 2005 1:22 am    Post subject: Wildcard dns and sub domains Reply with quote

What follows is what I consider to be best practice for my personal sites and a guide for those who wish to do the same. Months ago I dropped the www. prefix from my domain in part because I think it’s redundant and also because I wanted to experiment with how Google treated valid HTTP redirect codes. The experiment has been a great success. Google seems to fully respect 301 Permanent Redirects and the change has taken my previously split PageRank has been combined and now I am at 7. There are other factors that have contributed to this, of course, and people still continue to link to my site and posts with a www. (or worse) in front of it, but overall it just feels so much cleaner to have one URI for one resource, all the time. I’m sure that’s the wrong way to say that, but the feeling is there nonetheless.

Now for the meat. What’s a good way to do this? Let’s look at our goals:
No links should break.
Visitors should be redirected using a permanent redirect, HTTP code 301, meaning that the address bar should update and intelligent user agents may change a stored URI
It should be transparent to the user.
It should also work for mistyped “sub domains” such as ww. or wwww. (I still get hits from Carrie’s bad link)

So we need a little magic in DNS and in our web server. In my case these are Bind and Apache. I am writing about this because at some point the code I put in to catch any subdomain stopped working and while I reimplemented it I decided to write about what I was doing. This method also works with virtual hosts on shared IPs where my previous method did not.

In Bind you need to set up a wildcard entry to catch anything that a misguided user or bad typist might enter in front of your domain name. Just like when searching or using regular expressions you use an asterisk (or splat) to match any number of any characters the same thing applies in Bind. So at the end of my zone DB file (/var/named/flashwebhost.com.db) I added the following line:

*.flashwebhost.com. 14400 IN A 122.206.122.43

Note the period after my domain. The IP is my shared IP address. 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 flashwebhost.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 flashwebhost.com. Here’s what I have:

Quote:

<VirtualHost 122.206.122.43>
DocumentRoot /home/flashhos/public_html
BytesLog domlogs/flashwebhost.com-bytes_log
User flashhos
Group flashhos
ServerAlias *.flashwebhost.com
ServerName www.flashwebhost.com
CustomLog domlogs/flashwebhost.com combined
RedirectMatch 301 (.*) http://flashwebhost.com$1
</VirtualHost>


The two magic lines are the ServerAlias directive which is self explanitory and the RedirectMatch line which redirects all requests to flashwebhost.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 cvs.flashwebhost.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.

That is it, I’m open to comments and suggestions for improvement.
_________________
HostOnNet.com - Reseller Hosting
Free Ads Forum, Promote your site
NetFreeHost.com - Free Web Hosting, phpBB Hosting
Back to top
View user's profile Send private message Send e-mail Visit poster's website AIM Address Yahoo Messenger MSN Messenger
Display posts from previous:   
Post new topic   Reply to topic    HostOnNet.com Forum Index -> Dedicated Servers All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © phpBB Group. Hosted by phpBB.BizHat.com


For Support - http://forums.BizHat.com

Free Web Hosting | Free Forum Hosting | FlashWebHost.com | Image Hosting | Photo Gallery | FreeMarriage.com

Powered by PhpBBweb.com, setup your forum now!