Zurück

Wildcard DNS and URL Rewrite


Overview

A Wildcard DNS record is a record in a DNS zone file that will match all requests for non-existent domain names, i.e. domain names for which there are no records at all. Using Wildcard DNS you can «correct» common mistypes of subdomains, for instance:

w.akadia.com
ww.akadia.com
akadia.com

all becomes www.akadia.com

If you want, that the Browser URL is also shown as www.akadia.com after a mistyped URL, then you can advise the Webserver to do an URL Rewrite.

Goals

These are the Goals using Wildcard DNS and URL Rewrite

  •  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 work for mistyped «sub domains» such as ww. or wwww.

DNS Setup

We need a little magic entry in the DNS configuration, in our case these is Bind (9.3.2). 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 I added the following line:

akadia.com. IN A 84.253.50.198
*.akadia.com. IN A 84.253.50.198

Note the period after the domain. The IP is the shared IP address. That’s all you need, now restart bind.

/etc/init.d/named restart

A very good resource to check the DNS Setup can be found on http://www.dnsstuff.com

Apache Setup

Now you need to set up Apache to respond to requests on any hostname under akadia.com. This can be done with the following directives:

<VirtualHost 84.253.50.198:80>
   .....
   ServerAlias *.akadia.com
   ServerName www.akadia.com
   .....
   RewriteEngine On
   RewriteCond %{HTTP_HOST} !^www\.akadia\.com [NC]
   RewriteRule (.*) https://www.akadia.com$1 [R=301,L]

</VirtualHost>

The magic lines are the ServerAlias directive which is self explanitory and the Rewrite Directives that redirects all requests to www.akadia.com