Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I've tried looking at other similar questions, but can't really find what I'm looking for.

I have a server running apache2, and I want all requests for to serve the pages in /var/www/foo.tld

For example:  -> /var/www/      -> /var/www/     -> /var/www/ -> /var/www/

I can't figure out how to do this. I can get it working sometimes with or without the www subdomain, but never with both.

share|improve this question

The VirtualDocumentRoot apache directive will do the trick, here is a good explanation :

share|improve this answer
Ah I see. Yes this would be the way to do it. – Tom H May 28 '12 at 14:25
How do I get this to work both with and without www subdomain? – Rory May 28 '12 at 15:49
@Blazemore I'd say symlink to – Shane Madden May 29 '12 at 1:32

I fixed the issue by creating a file /etc/apache2/sites-enabled-000-Dynamic

And renaming the file 000-default to 001-default.

In this file is the following

<VirtualHost *:80>

  RewriteEngine On

  # Redirect www to non-www (class B compliance with no-www)
  RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
  RewriteRule ^(.*)$ http://%1$1 [R=301,L]

  # Remove trailing slashes
  RewriteRule ^(.+)/$ http://%{HTTP_HOST}/$1 [R=301,L]

  # Automatically get the correct folder
  UseCanonicalName Off
  VirtualDocumentRoot /var/www/%1


I changed it slightly: the name now points to /var/www/foobar which makes it a lot easier.

As well as the dynamic virtual hosts, this also redirects to, and remov

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.