Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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 ^(.*)∞         http://www.example.com/$1 [L,R=permanent]
</Directory>

which works great when accessing example.com.

But if I make a symlink ln -s /path/to/somewhere foo and I try to access example.com/foo I doesn't get redirected to www.example.com/foo 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

1 Answer 1

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
</Directory>
<Location />
  RewriteEngine on
  RewriteCond %{HTTP_HOST} ^example\.com
  RewriteRule ^(.*) http://www.example.com/$1 [L,R=permanent]
</Location>

...in 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 example.com/sysinfo (which is a symlink) I'm redirected to www.example.com/var/www/main/sysinfo –  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

 
discard

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.