Monday, July 19, 2010

Wildcard DNS Redirect

SkyHi @ Monday, July 19, 2010
If you try going to any of the pages on this site, and change the "www" to a domain of your liking, you'll end up back on the same page. That is because I've done the following steps for this site. In fact, I'm writing this article more so I'll remember how I did it, and making it available to you in case you want to know too.
I am running WHM/cPanel and Apache, so I'm going to tell you how to do it using these. You can do it with other control panels, but the paths and way you edit your dns may be different. Please also note that this site has a dedicate IP. It may be slightly different if you don't, but you should be able to get the idea behind it and implement it yourself.
Step 1 - Wildcard DNS Entry
You'll need to login to WHM (or your control panel), and find the "Edit a DNS Zone" link under "DNS Functions". Select the domain you are making the wildcard for, and click Edit.
If you want to redirect to "www.yoursite.com", edit the CNAME entry for www to be: (change 192.168.0.1 to your site's IP)
"www.yoursite.com." "A" "192.168.0.1"
Next you'll want to create a new "A" entry, same as above only it will be "*.yoursite.com." -- Notice there is a period after the .com -- Not having it there can lead to some problems.
If you aren't using WHM, remember to restart bind. WHM does it automatically when you click save.
Step 2 - Telling Apache
Now you are going to have to tell Apache that something new is going on. You'll need to edit your httpd.conf file. The location can differ, but here is a common path:
/etc/httpd/conf/httpd.conf
I edit it using pico with the -w attribute (no wrap). If you have a different preference thats fine. Once you are in there, find the Virtual Host entry for your site. In pico you can hit CTRL W and type your domain (include www if you have subdomains to find it faster). Before that VirtualHost entry, make a new one like so:

ServerAlias *.theclixnetwork.com
RedirectMatch 301 (.*) http://www.clixnetwork.com$1
Remember to change the IP and domain to yours. This will do the actuall redirecting. The 301 tells the browsers and search engines it is a permanent redirect. You can use "permanent/301" (the resource has moved permanently), "temp/302" (the resource moved temporarily), "seeother/303" (the resource has been replaced).
If you've noticed Google can have a different Page Rank for www.yoursite.com and yoursite.com - Thats because to Google its a different site, and some people link to one or the other. Using the permanent redirect, Google essentially combines the PR because it sees the site as one.
Ok now you'll want to make sure that the original virtual host entry only has "ServerAlias www.theclixnetwork.com", and that it comes after the above entry. Otherwise you'll find like me that it just keeps redirecting endlessly.
So there you go, you can now redirect all your subdomains to your main site. I would like to find out if it is possible to do so without editing the httpd.conf file, and using .htaccess instead. I'm sure you can, but thats another article.


REFERENCES
http://www.timlinden.com/blog/website-development/wildcard-dns-redirect/