Posts Tagged ‘PowerDNS’

Enabling URL-records in PowerDNS

Posted in PowerDNS on September 29th, 2009 by webern – 2 Comments

Tired of setting up small web hotels just so you can redirect domains to another url? Well, so was I.
Luckily we’re using PowerDNS which supports URL-type record. What it basically do, is telling your PowerDNS nameserver that you have a fancy record it should do something with.

Let’s get started:

  • First of all, you should enable fancy records in PowerDNS. Open up pdns.conf and paste this:
    fancy-records=yes
  • Then, you should tell it where your redirector-service is, replace 192.168.1.1 with your publically accessible redirector-service:
    urlredirector=192.168.1.1
  • Next up is to enable your webserver to redirect the requests. First, add a new virtual site. How you do this varies between different distros I’ll leave that up to the reader. Here’s my current virtual host for it:
    <VirtualHost _default_:80>
    ServerAdmin hostmaster@localhost
    DocumentRoot /var/www/html/urlredirect
    ErrorLog logs/urlredirect-error_log
    CustomLog logs/urlredirect-access_log common
    </VirtualHost>
  • Next, and finally, create a simple php-page that actually does the redirecting. It will connect to the MySQL-backend of PowerDNS, read the URL-record and redirect the user to that location.
    index.php
    <?php
    
    // Get the server name our user requested
    $servername = $_SERVER["HTTP_HOST"]; 
    
    // replace this with credentials to your powerdns database
    @mysql_connect('db-host','username','password');
    // replace this with the right database name
    @mysql_select_db('powerdns');
    $result = @mysql_query("SELECT content FROM records WHERE name = '$servername' AND type = 'URL' LIMIT 1");
    // Query for our redirection
    if (mysql_num_rows($result) == 1) {
    // we got a result, redirect to that
    $row = @mysql_fetch_object($result);
    header("Location: $row->content");
    } else {
    // we didn't get any result for unknown reason, redirect to failsafe place
    header("Location: http://www.google.com");
    }
    ?>
  • This should be it! If you’re really brave you could also add a rewrite rule to make sure all requests get redirect, not only those to somedomain.com/