Archive for the ‘BIND’ Category

Domain Redirection using Apache mod_rewrite and .htaccess

Saturday, January 26th, 2008

I recently acquired more XenoCafe domains (xenocafe.info, xenocafe.net, and xenocafe.org) and finally got around to adding them to my server. Instead of them being their own sites (well, point to my primary site, but the URL in the browser address bar is taken over by the new domain name), I wanted them to redirect to my primary domain (xenocafe.com).

If you want to achieve the same effect, here is how I did it (to see it in action, click on the first three XenoCafe links above). All you need is Apache, mod_rewrite, and htaccess. I’m going to skip the step-by-step configuration stuff to cut down the length of this article. If you need help, you can register for an account and post a question to the comments section for this post.

In this mock setup, I’ll use 10.10.100.34 as the IP address of my web server, example.com as the primary domain I want to redirect to, and ourexamples.com as the domain I want to redirect from (to example.com). This is a three step process that involves creating the zone file(s), adding the redirect domains to your Apache httpd.conf VirtualHost configuration, and finally creating the htaccess mod_rewrite rules to redirect all requests for the new domains to your desired domain.

Create a New Zone File for each Domain you Acquired

The primary domain example.com already has a zone file and is a functioning web site, but for ourexamples.com to work we need to create a zone file for it. Here is an example (we only need the minimum – SOA, NS, and the host for web).

$TTL   21600
$ORIGIN ourexamples.com.
@       IN      SOA     ns1.example.com. hostmaster.example.com. (
                        2008012601      ; serial
                        3600            ; refresh
                        600             ; retry
                        86400           ; expiry
                        21600 )         ; minimum

        IN      NS      ns1.example.com.
        IN      NS      ns2.example.com.

        IN      A       10.10.100.34
www     IN      A       10.10.100.34

Now reload the zone(s) for changes to take effect.

Add the Redirect Domains to the Primary Domain’s VirtualHost Definition (using the ServerAlias directive)

Edit your primary domain’s virtual host entry and add the new domain(s) using the ServerAlias directive. Below is an example of editing the example.com virtual host and adding the new redirection domain ourexamples.com (highlighted line).

<VirtualHost *:80>
    ServerAdmin hostmaster@example.com
    ServerName example.com
    ServerAlias www.example.com
    ServerAlias ourexamples.com www.ourexamples.com
    DocumentRoot /web/example/html
    ScriptAlias /cgi-bin/ /web/example/html/cgi-bin/
    ErrorLog /web/example/logs/error_log
    CustomLog /web/example/logs/access_log combined
    <Directory "/web/example/html">
        AllowOverride All
    </Directory>
</VirtualHost>

Restart Apache to reload the virtual host changes. Next we’ll need to create the htaccess file to configure domain redirection.

Using mod_rewrite to Redirect the Domain Requests

If we were to stop here, the new domain would work, however there is no redirection to your primary domain name. It will simply use the same document root and serve the files without any changes to the URL in the browser address bar. If this is what you desire, then stop here, otherwise continue on to bounce all requests from ourexamples.com to example.com.

In the document root of where your HTML files are stored (where DocumentRoot points to in your Apache VirtualHost definition), create a .htaccess with a text editor. Here is the example.

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule (.*) http://www.example.com/$1 [L,R=301]
</IfModule>

In short, what this code does is turn on mod_rewrite’s engine, check the HTTP host to see if it matches www.example.com and also checks for empty references; if either case is a yes it rewrites the URL to http://www.example.com using a permanent redirect (HTTP 301 Redirection header). The $1 appends any portion trailing the domain name from the original (directories and/or pages being accessed). This code also forces the ‘www’ prefix on all requests — http://example.com => http://www.example.com and http://ourexamples.com => http://www.example.com and so on

What happened to named.conf in ISC BIND DNS on Fedora 7?

Sunday, December 16th, 2007

I’ve made the great leap this week by moving to a new colocation. This site and many others have moved to a new dedicated server because the old service I used provided by unnamed company was absolutely horrible. They boasted 99.99% uptime, HA! I was lucky to get 75% uptime out of those fools for the year I was with them. Anyway…

The one thing I hate about moving is having to transfer gigs of data and reconfigure things. To cut it short, the old server was Fedora Core 4 and the new server is Fedora 7. Changes have been made between the three versions difference. One of these changes explains the title of this post. What happened to named.conf?

I checked the typical locations like /etc and /var/named/chroot/etc but it was nowhere to be found. I ran a locate and it didn’t exist. Thinking it was somewhat possible that maybe it was in a RPM I hadn’t installed, I checked the RPM list on a Fedora mirror site and compared those to the RPM’s I did have installed. No luck, everything was there.

I searched the net and didn’t find any reason for named.conf’s disappearance. I also discovered I wasn’t the only one seeking an explanation to its vanishing act. I came across a post on a forum mentioning the use of system-config-bind to create named.conf. A marvel idea, however it’s a GUI based tool and that leaves us text based users out in the cold — well sort of…

If you’re like me and want to set up a DNS Server on Fedora 7 to handle your zones, then follow these steps while as root.

1. Even though it’s a GUI dependent tool, install system-config-bind using yum. It contains template files you can use to get named up and running.

yum install system-config-bind

2. The templates used by system-config-bind for generate named.conf and other essential files have been installed in /usr/share/system-config-bind/profiles/default. We need to copy these files to where named would be expecting them (the assumption being that you’re running named in a chroot jail.

cd /usr/share/system-config-bind/profiles/default
cp -p named.conf /var/named/chroot/etc/
cp ./named/* /var/named/chroot/var/named/

3. Now that named.conf is copied over we’ll need to make a change to the root hints file name. Right now our named.conf is looking for the file named.root instead of named.ca as in prior releases of bind. If you don’t make this change, named will fail to start and you’ll get an error like this in syslog named[20622]: could not configure root hints from ‘named.root’: file not found. Open named.conf in a text editor and look for this section (it should be right below options).

zone "." IN {
        type hint;
        file "named.root";
};

Change named.root to named.ca and save your changes.

4. Create a symlink to named.conf in /etc (optional but suggested).

ln -s /var/named/chroot/etc/named.conf /etc/named.conf

5. Start the named service and you’re good to go.

service named start

Now go ahead and create your zone files and add their references to named.conf. See my RedHat Bind Tutorial on XenoCafe for more information on DNS and Zones.