Wednesday, February 20, 2013

Configuring Apache and DNS to access site without www

When hosting a site, it's convenient to just type the domain name e.g. instead of pre-fixing it with www.  This can be easily configured using Apache and by adding one additional entry to the DNS.  For this article's purpose, we'll be using as the domain.

To start off with, add a new A DNS entry to point to the same IP as the A entry which maps to  You'll have something like below:

Name Type Value A A

Once that's done, we configure Apache.  Inside the VirtualHost directive, add the Alias directive as well as the rewrite condition:

<VirtualHost *:80>

DocumentRoot /home/blah/public_html
        <Directory /home/blah/public_html/>
AllowOverride All
Order allow,deny
allow from all

RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [L,R=301]


The rewrite condition redirects all requests beginning with www to the domain without www prefix.

No comments: