1

I am coming from IIS and setting up virtual directories there is so pleasantly easy. You just right-click and add a "virtual directory" pointing to any path you like. This relative path gets reflected through the browser when viewing the site.

How do I accomplish the same thing with Apache? Symlinks scare me because deleting cascades through. Is there a better way to create virtual directories in Apache? I am using MAMP for Mac which uses apache2.

3 Answers 3

4

Use the Alias directive in the Apache configuration file.

Alias /virtual-dir /var/www/foo

Additionally, rm -r does not traverse symlinks and will not cascade through.

0

If you point multiple domains to the same public html directory, and the actual content resides outside of the public html (web root), you can do this in two steps:

1) rewrite the directory per domain, e.g.

<VirtualHost 127.0.0.1:80>
  ServerName host_one.ca
   RewriteEngine On
    RewriteRule "/?images(.*)" "/www/shared/host_one/images/$1" [NC]
</VirtualHost>
<VirtualHost 127.0.0.1:80>
  ServerName host_two.ca
   RewriteEngine On
    RewriteRule "/?images(.*)" "/www/shared/host_two/images/$1"
</VirtualHost>

2) Then add symlinks (alternatively, the Alias)

ln -s /shared/host_one/images /www/html/images/host_one
ln -s /shared/host_two/images /www/html/images/host_two

Just be careful. host_two images can be accessed via host_one: domain_one.ca/images/host_two/{filename}

Unless you add additional rules that anything NOT in the current host directory be remove. For the host_one VirtualHost (I did not confirm whether this rule works):

    # prevent showing content from other hosts
    RewriteRule "/?images/!(host_one/.*)" /404 [NC,L]
0

If it's a Secure Server the 'Alias' directory needs to go to in the VirtualHost 443 config as well

<VirtualHost *:443>
...
Alias /virtual-dir /var/www/foo
</VirtualHost>

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .