views:

542

answers:

1

I have this .htaccess file where I prevent users from physically accessing files from the browser (where they should only be loaded through the system)

Options -Indexes
Order deny,allow
deny from all

I have one problem though, sometimes I load files via AJAX and there I get 403 Forbidden. I have little experience with apache's mod_access. I've been reading up on the directory directive since all my AJAX based files are in one directory called ajax.

But the thing is I need to deny access to all directories except ones called ajax and my regex skills are lacking.

An example directory structure is like this.

plugins/inventory/ajax
plugins/inventory/controller
plugins/inventory/view

plugins/packages/ajax
plugins/packages/controller
plugins/packages/view

The .htaccess file sits in the plugins directory.

+1  A: 

That you need to do this in the first place is kind of a failure of project architecture. Script files that shouldn't ever be accessible to the Web shouldn't be inside your DocumentRoot in the first place.

That said, this will probably work:

RewriteEngine on
<DirectoryMatch "/(?!.*/ajax$)">
   Order deny,allow
   Deny from all
</DirectoryMatch>
chaos
Thanks for the tip on that, the reason for this is that people are writing plugins that are automatically loaded into the system and should be able to drop into a location.
Ólafur Waage
Hmm, interesting. So, what, they need to be inside the DocumentRoot because they're publishing via HTTP PUT or something?
chaos
I get your point and it could be possible we could implement the plugins in a different way. They are just directories within the system developers can use to program addons for the system.
Ólafur Waage
Seems like I get a 500 Internal Server Error with anything I try. Both <Directory> and <DirectoryMatch> and multiple combination of items from the documentation and your answer.
Ólafur Waage
Check your server/vhost error log to see what the actual error is. Possibly you don't have mod_rewrite loaded so the RewriteEngine directive is breaking?
chaos
@Ólafur: Both those directives aren't allowed inside the `.htaccess`, only in the server config. See their "Context" property in the manual page you linked to yourself.
mercator