Question

I have a local server environment running on my OS X machine using MAMP, and I am currently setting up a few virtual hosts. I found that, in order for one of my virtual hosts to recognize my site's .htaccess file, I needed to add a <Directory> directive within the <VirtualHost> directive.

Here is an example of what I am using to configure one of my virtual hosts:

<VirtualHost *>
    DocumentRoot "/path/to/mysite"
    ServerName mysite.local
    <Directory "/path/to/mysite">
        AllowOverride All
        Order Allow,Deny
        Allow from all
    </Directory>
</VirtualHost>

Now, I would like to avoid redundancy by removing the path from that <Directory> directive. I tried doing so and it seems to work, although I am not familiar with Apache enough to know the potential consequences of doing this. Furthermore, I could not find an explanation of this case in the Apache Documentation.

This is what I would like to do:

<VirtualHost *>
    DocumentRoot "/path/to/mysite"
    ServerName mysite.local
    <Directory>
        AllowOverride All
        Order Allow,Deny
        Allow from all
    </Directory>
</VirtualHost>

Therefore, my question is this: what happens when I omit the path from the <Directory> directive (in this case)?

Was it helpful?

Solution

Directory is a directive targeting a file system directory, and should always have a path (waildcards allowed).

Using some instructions targetd to no directory may be discarded silently, or maybe with an error (you could test it), but is would certainly be useless.

Why would you write some configuration instructions targeted to no directory at all?

But there's maybe more things in your question.

Ususally a Virtualhost should contains at least one Directory instruction, the DocumentRoot directory, and usually we also add one targeting the root of the filesystem, this way:

<Directory />
    (instructions to deny access and avoid .htaccess reads)
</Directory>
<Directory /my/document/root/>
    (instructions to allow access and still avoid .htaccess reads or to allow them if you are lazy and feel over confident that .htaccess are useful in any way and you do not need speed)
</Directory>

And you could add more, especially to avoid usage of .htaccess files as such file stored in /path/to/somewhere is the same thing as a <Directory /path/to/somewhere> section, but slower.

Now you do not like to repeat yourself in your VH configuration, so you coud maybe use .htaccess files or global level instructions for things that are usually set there. And you could also use another magical keyword, which is Location.

You can use a lot of Apache instruction in a <Location /foo> directive. Locations works on url, not on filesystem paths.

So <Location /> is your documentRoot, and <Location /foo> is maybe the subdirectory 'foo' under you DocumentRoot. This could help you write things in your VH without reusing the documentroot prefix everywhere. But be careful, urls are not file-system paths. Any protection applied on the url level may be weaker or stronger compared to a protection applied on a directory level. It depends on your alias, the way you use urls, etc.

Update:

If you use the new Apache 2.4 version you can now use mod_macro or even easier, built-in variables support:

Define docroot "/path/to/mysite"
DocumentRoot ${docroot}
<Directory ${docroot}>
    (...)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top