I am trying to deny access to the root directory while allowing access to certain files (ending in .html and .js) and specifying a DirectoryIndex so requests for the directory will respond with a default page.
Checked these things already:
-Apache/2.2.14 can read its directory root.
-The DocumentRoot file exists.
-There are no other directory settings which configure the same directory
Here's one of the more explicit configs I tried.
<VirtualHost *:80>
ServerName myserver.com
DocumentRoot /var/www/client
DirectoryIndex app.html
<Directory /var/www/client>
AllowOverride None
Options +Indexes
Order Allow,Deny
Deny from all
<Files ~ "\.(js|html)$">
Order Deny,Allow
Allow from all
</Files>
</Directory>
</VirtualHost>
Gives a 403. I tried a couple of permutations, trying to be as explicit with directives as I knew how. I tried allow from all
on the directory to prove there were no conflicting rules and the DirectoryIndex worked as expected (served app.html). With the above config I can get /app.html, just not have it served as the default for requests to "/".
This is what I ultimately did to get the desired effect.
<VirtualHost 127.0.0.0:80>
ServerName example.com
DocumentRoot /var/www/client
AliasMatch ^/$ /var/www/client/app.html
<Directory /var/www/client>
AllowOverride None
Order Allow,Deny
Deny from all
<Files ~ "\.(js|html)$">
Order Deny,Allow
Allow from all
</Files>
</Directory>
<VirtualHost>
Is there a better way to achieve my goal? Does DirectoryIndex not work with ?
Best Answer
Using an AliasMatch is a good way to achieve what you want.
The reason why DirectoryIndex doesn't give you what you want is that the
Files
directive looks at the request made by the browser, and that request does not contain .html or .js. TheAliasMatch
directive will ensure that the request gets rewritten before reaching theFiles
directive - but the DirectoryIndex doesn't happen in the proper context for it to be caught by theFiles
directive.