act:ualise | technology

23 Nov, 2008

apache domain to subdomain redirection

Posted by: j pimmel In: linux|technical

In setting up this site, I was recently struggling with the issue of how to redirect say

      http://domain.com

to

      http://sub.domain.com

without it causing an infinite recursion loop when visiting

      http://sub.domain.com/foo

In the end my VirtualHosts config looked like this

NameVirtualHost *:80
<VirtualHost *:80>
    ServerAlias sub.domain.com
</VirtualHost>
<VirtualHost *:80>
    ServerAlias domain.com
    ServerName domain.com
    Options +FollowSymLinks
    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^domain\.com
    RewriteCond %{REQUEST_URI} -d
    RewriteRule .* http://sub.domain.com%{REQUEST_URI} [L,R=301]
</VirtualHost>

There is a lot of noise to signal when it comes to Google results for apache redirect/rewrite variables and their meaning, but this cheatsheet should help you tremendously.

No Responses to "apache domain to subdomain redirection"

Comment Form

About

act:ualise | technical blog

notes, observations and obssessions on software quality, agile software development, testing and grails


part of the EnergizedWork experiment