Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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'm trying to force the adding of www. if visiting my site. I've added the following to my site conf file:

<Directory /var/www/main>
    Options FollowSymlinks
    RewriteEngine on
    RewriteCond %{HTTP_HOST}   ^example\.com
    RewriteRule ^(.*)∞$1 [L,R=permanent]

which works great when accessing

But if I make a symlink ln -s /path/to/somewhere foo and I try to access I doesn't get redirected to which is of course as expected since /path/to/somewhere is not in /var/www/main

Do you have any suggestions on how to make it work with symlinks as well?

share|improve this question
up vote 1 down vote accepted

You're hitting the difference between Directory and Location directives in Apache. According to the documentation, Directory applies to filesystem-space, and Location applies to web-space. Try breaking your configuration block up like so:

<Directory /var/www/main>
  Options FollowSymLinks
<Location />
  RewriteEngine on
  RewriteCond %{HTTP_HOST} ^example\.com
  RewriteRule ^(.*)$1 [L,R=permanent]
</Location> other words, pull the rewrite section out into a Location block.

share|improve this answer
This redirects but posed a new problem: The if I go to (which is a symlink) I'm redirected to – Nicklas Ansman Sep 25 '10 at 15:51
Ah, the trick was to skip the Location and Directory all together and put it directly inside <VirtualHost> – Nicklas Ansman Sep 25 '10 at 15:57

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.