VirtualHost Hacking with Wildcard DNS
Posted by Nessa | Posted in apache,dns,tutorials | Posted on May 19, 2007
7
A recent topic in the webhosting business is wildcard DNS. Setting up a wildcard record will allow you to essentially have infinite subdomains all pointing to the same place…. you can try this on my site if you’d like by typing in any random subdomain (like lkjairl.v-nessa.net) and then see if point back to my primary, unless it’s one that I’ve actually created. Now if you combine this ‘technology’ with the power of .htaccess rewrites, then congratulations. You’ve just cheated your host and obtained unlimited subdomains.
The first thing you would need to do is have a wildcard DNS record set up. If you have access to your own zone files, that’s great, otherwise you’ll need to ask your host to set it up for you. Fair warning though, your hosting company is not stupid (unless it’s Dreamhost) so don’t be surprised if they decline your request. To set up the DNS zone you simple need to add an a-record “*” like so:
* 14400 IN A 205.134.252.71
*Note: Some argue that you need to use the full *.domain.com. as the first field…this is actually not required at all, nor recommended!
Depending on your platform, you’ll likely find your domain’s zone file in /var/named/domain.com.db unless you have this feature in your host’s control panel. Once you’ve added the record simply reload named/BIND with the /etc/init.d/named reload command if needed. If you’re using an interface it will probably do this for you automatically.
Now you need to add ServerAlias line to your httpd.conf for the wildcard, if your host does not already have Apache configured this way. Inside the <virtualhost> tags for your domain, add this line:
ServerAlias *.domain.com
Then restart Apache as usual (/etc/init.d/httpd restart) and test a random subdomain to see if it’s working.
So now you will notice that any non-distinguished subdomain shared the same document root as your primary….that’s great and all but it really serves no purpose, so that is where mod_rewrite comes in. You can add and modify the follow code in your .htaccess to have Apache direct these subdomains to where they need to go:
RewriteEngine On
RewriteRule ^\.htaccess$ – [F]
RewriteCond %{HTTP_HOST} !^www\.example\.com?$
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com?$
RewriteRule ^$ /folder/page.html [L]
Using your imagination you can write your own script that can create subdomains and the appropriate .htaccess entries on-demand (assuming that this is not blocked by mod_security). The end result is seemingly unlimited subdomains on your account! Also, the changes are usually immediate and do not require propagation.
No related posts.












[...] v-nessa.net » VirtualHost Hacking with Wildcard DNS [...]
[...] some additional information on the files you need to edit at V-nessa.net. Note that the files may not be located where specified depending on your hosting provider. My [...]
[...] some additional information on the files you need to edit at V-nessa.net. Note that the files may not be located where specified depending on your hosting provider. My [...]
[...] some additional information on the files you need to edit at V-nessa.net. Note that the files may not be located where specified depending on your hosting provider. My [...]
[...] 參考資料: Wildcard DNS dynamic subdomain VirtualHost Hacking with Wildcard DNS Dynamic Sub-Domain with PHP WMPU Subdomain rewriting with MAMP dp.SyntaxHighlighter.ClipboardSwf [...]
Could you tell me, which file ist the config file for putting this line?
* 14400 IN A 205.134.252.71
I have a 1und1 root server with plesk installed.
U can mail me too.
Thanks a lot!
@Miles that would be in the zone file of your nameserver