O'Reilly logo

Linux Server Hacks by Rob Flickenger

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Hack #96. Serving multiple sites with the same DocumentRoot

Through creative use of mod_rewrite, several sites can share a DocumentRoot and yet still appear to be independent sites

Occasionally, it is useful for a couple of sites to share the same DocumentRoot but have unique front pages. Perhaps you want to share a graphics or javascript directory, but for some reason can't be bothered to set up an appropriate Alias entry. Or maybe you need to be able to share most of a directory structure, but don't want to FollowSymLinks. Whatever your reason for needing a common DocumentRoot, mod_rewrite will provide a solution.

Let's assume that you are running a site called www.fruit.yum, and would like to serve information about apples and oranges. You've already had much success with http://www.fruit.yum/apples and http://www.fruit.yum/oranges, but you are suddenly able to obtain the much coveted apple.yum and orange.yum domain names. You'd like to keep the existing www.fruit.yum directory structure for users who browse to directories under your new domain names (for example, to http://www.apple.yum/order or http://www.orange.yum/faq), but would like to deliver a custom page to users who simply browse to the front page (e.g., http://www.apple.yum/).

This is no problem with mod_rewrite. Let's assume that you already have a VirtualHost entry for www.fruit.yum like this:

<VirtualHost *> ServerName www.fruit.yum ServerAdmin webmaster@fruit.yum DocumentRoot /home/www/htdocs CustomLog /home/www/logs/access_log ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required