Customizing Domains With Wildcard DNS
It seems to be the “in” feature of most modern web apps today. Giving your users the ability to create a custom URL that they can use to access their account like http://chickenbbq.sign-up-sheet.com. From a feature stand point its nice because it’s another way users can customize your app to their specifications. From a developers perspective however it can be a bit challenging to implement if you don’t know what your doing.
When I was developing my first web app (version 1 of BadgeTracker) I really wanted to implement this feature, but for the life of me I couldn’t figure out how to do it. I didn’t even really know what it was called so I had a hell of a time searching for information. I then built Sign-Up-Sheet.com and still had no idea how to accomplish the effect, but I really needed to because each sign-up-sheet needed to have it’s own address and a dynamic address would have been perfect. I don’t really recall how I finally figured out what I was looking for, but the answer to my prayers was Wildcard DNS. In this post I will walk you through implementing Wildcard DNS with your next web app.
Disclaimer
Before I get started I just wanted to mention that I do not claim to be a networking person. I don’t enjoy networking or server administration or anything like that… I’m software not hardware
. However, this was a topic that I personally struggled with when starting out and I wanted to pass along what I have found and done.
What is wildcard DNS?
Wildcard DNS is typically defined as: an address record you can place in the DNS for your domain that will send all subdomains (not otherwise declared in your DNS) to a specified IP address. The concept is simple once you figure it out. Adding a Wildcard DNS record to your existing host records looks something like this:
*.christophermonnat.com A 10.5.128.1
The astrisk in this case is the wildcard character. This A record directs all subdomain requests to the IP 10.5.128.1. So now all you have to do is place you web app at the same IP address and you will be well on your way.
Do I need a dedicated IP?
It depends on how your app works. With Sign-Up-Sheet I have one CodeIgniter installation running 2 parts of the site. There is the website/admin area where users can browse the features of the program or sign-in to administer their account. And there is the actual public sign-up-sheet’s themselves which need to be displayed when a user enters the specified sheets address into the browser window. Because my app has 2 parts but is using the same CI installation I decided to use a shared IP for Sign-Up-Sheet.
With BadgeTracker there is only one CI installation but it has nothing to do with the website. The website and application are 2 physically separate things so the easiest way to facilitate this architecture was to get a dedicated IP for the application and leave the website with a shared IP. All subdomains go to the dedicated IP while the WWW record points to the shared.
When asking yourself this question I guess the determining factor boils down to how many parts there are to your app and whether they all belong to a single CI installation or many separate ones.
Wildcard DNS and CI
As I mentioned above, with Sign-Up-Sheet I have one CI install that handles both the display of sign-up-sheets decided by the URL and the front-end website/admin area. Since I’m using a shared IP I chose to have CI handle the determination of what to display to the user and when.
When doing something like this, there are 2 files you need to be concerned with: config/config.php and config/routes.php. In config.php, the issue is the base_url variable. This needs to be set to the home of your app in order for CI to operate properly. The problem with our implementation however is that it may change depending on what the user is requesting. Sometimes it will be a subdomain and other times it will be straight WWW. One way to account for this is to generate the base_url setting dynamically.
[code]
$config['base_url'] = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == "on") ? "https" : "http");
$config['base_url'] .= "://".$_SERVER['HTTP_HOST'];
$config['base_url'] .= str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']);
[/code]
The above example is from the CI Wiki. By placing this code in your config.php file, the base_url will be dynamically generated based on what is being requested thereby solving that problem. The other file we need to be concerned with is routes.php. The issue here is that we want to serve the user with a different default controller if they are visiting the site using a subdomain vs. just WWW. We can accomplish this with the following code:
[code]
if(preg_match("/^([a-zA-Z0-9\-_]+)\.([a-zA-Z0-9\-_]+)\.([a-zA-Z]{2,5})$/",$_SERVER["SERVER_NAME"],$matches))
{
list($subdomain,$domain,$tld) = $matches;
}
if($domain == 'www'):
$route['default_controller'] = "main";
else:
$route['default_controller'] = "signup";
endif;
[/code]
The above code looks at the URL and gets just the subdomain portion. We then look at the subdomain and determine if its WWW or something else. From there we decide which default controller to set for the application. Once placed in your routes.php file this accomplishes our goal nicely. Now that we are pointing our users to the correct controller we can refer to our database to find the records associated with the requested subdomain and proceed from there.
Conclusion
Wildcard DNS is available with most popular DNS providers like Enom, Inc. and GoDaddy and is also available with popular control panels like Plesk. If your not sure if your provider offers Wildcard DNS you should ask because there are some out there that don’t. Something I did want to mention about using Wildcard DNS is that it’s not necessarily the answer to all of your problems and should be implemented after careful consideration. The makers of Wufoo, a popular app that makes hosted web forms, wrote a post a while ago about the issues they experienced while working with this kind of setup. It’s a must read before adopting this concept for your own apps.
Well, there you have it Wildcard DNS demystified. If you have any questions or if you are a networking/DNS genius and you see something I have gotten terribly wrong please post a comment and let me know.
Related posts:
If you enjoyed this post, please consider to leave a comment or subscribe to the feed and get future articles delivered to your feed reader.
![Reblog this post [with Zemanta]](http://img.zemanta.com/reblog_e.png?x-id=25d3cf69-5471-453f-8e85-68fd98e7ad97)

Hi, I found your blog on this new directory of WordPress Blogs at blackhatbootcamp.com/listofwordpressblogs. I dont know how your blog came up, must have been a typo, i duno. Anyways, I just clicked it and here I am. Your blog looks good. Have a nice day. James.