From domain.com to www.domain.com in Apache

August 3, 2009

While redirecting from domain.com to www.domain.com is quite common, I hadn’t done it until the day before yesterday. I wanted to redirect all web access towards rosedu.org to www.rosedu.org. Apart from the cosmetic effect, the main reason was the configuration of a digital certificate for HTTPS access for the www.rosedu.org domain. As the certificate’s common name (CN) was www.rosedu.org a warning appeared when using HTTPS for the rosedu.org domain.

The initial configuration is quite basic: two virtual hosts, one for HTTP access (www.rosedu.org) and one for HTTPS access (www.rosedu.org-ssl), as shown below:

==> www.rosedu.org <==
<VirtualHost *:80>
ServerAdmin sergiu@rosedu.org
ServerName rosedu.org
ServerAlias www.rosedu.org

==> www.rosedu.org-ssl <==
<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerAdmin sergiu@rosedu.org
ServerName rosedu.org
ServerAlias www.rosedu.org

A little browsing revealed the solution: the use of the almighty mod_rewrite in Apache. The rule needed only be applied to rosedu.org requests so I needed a condition for selecting that domain name. Quite easy to do, I ended up with the following configuration:

==> www.rosedu.org <==
# Redirect rosedu.org to www.rosedu.org
RewriteEngine On
RewriteCond %{HTTP_HOST} ^rosedu.org$
RewriteRule (.*) http://www.rosedu.org$1 [R=permanent,L]

==> www.rosedu.org-ssl <==
# Redirect rosedu.org to www.rosedu.org
RewriteEngine On
RewriteCond %{HTTP_HOST} ^rosedu.org$
RewriteRule (.*) https://www.rosedu.org$1 [R=permanent,L]

The %{HTTP_HOST} directive allows selection of the hostname (using ^ (begin) and $ (end) regular expression symbols). The request URL ($1) is added to the new domain name (www.rosedu.org).

If you want to redirect requests towards www.rosedu.org to rosedu.org, you would just have to substitute the domains in the above configuration.

As I wanted to use an easy'n'sneaky way out, I had made a mistake trying to use the Redirect directive from mod_alias:

Redirect permanent / http://www.rosedu.org

However, when accessing http://rosedu.org, the URL transformation did take place (the browser now showed http://www.rosedu.org in its location bar), but the web server greeted me with an error message: This page cannot load because of a problem with the Web site. Accessing the logs showed a large number of redirect messages ("GET / HTTP/1.1" 301 229 ") revealing the problem: the web server was now recursively redirecting http://www.rosedu.org to the same URL. This happened because the virtual host was responsible both for the rosedu.org domain (the ServerName directive) and www.rosedu.org domain (the ServerAlias directive). Oh well, live and learn!

One extra thing I found out is the possibility of selecting HTTPS or non-HTTPS traffic through the use of the %{HTTPS} variable or the %{SERVER_PORT} variable (assuming HTTPS uses 443):

RewriteCond %{HTTPS} !on
RewriteCond %{SERVER_PORT} !^443$

Besides the official Apache documentation for mod_rewrite, the mod_rewrite Variables Cheatsheet is a valuable resource.

tags: , ,
posted in Administration by Răzvan Deaconescu

Follow comments via the RSS Feed | Leave a comment | Trackback URL

Leave Your Comment

*

*

 
Powered by Wordpress and MySQL. Theme by Shlomi Noach, openark.org