13 March 2012

Apache "AllowOverride None" confusion

Background

We had a situation where, there was a Apache Server with virtual hosts configured, one of the virtual hosts  was located at

eg. /var/www/mysite

Under there was another media folder called media.

/var/www/mysite/media

I wanted to serve the media as a separate virtual host.  The basic configuration was easy enough, just create a separate symlinked virtual host configuration file in /etc/apache2/sites-enabled for the new media site.

What went wrong

Everything seemed to work fine if the files (jpgs) were found in the media folder they were returned to the client as expected, however if a file was missing the server was generating server redirection errors, instead of 404s.

This was causing me to tear my hair out.  Why redirection errors?  There was no .htaccess file in the media folder, and in the directory section in the virtual host configuration "AllowOverride None" was set.  So I was reasonably expecting that no .htaccess file would be processed for this virtual host, and as there wasn't one in the media folder anyway, I couldn't understand which rules was it processing to end up with redirection errors.

What was really happening

AllowOverride None meant that no .htacces directives in the media folder were being applied, but because there was an .htaccess in the parent folder for the site virtual host, those rules were being applied instead, which is where the redirect rules were located causing the redirect fail.

How to fix

Adding another directory section to the virtual host config did the trick, this prevented the parent .htaccess file from being processed.

[Directory /]
   AllowOverride None 
   Options None
[/Directory]

*angle brackets changed because of blogger

(If you want to be really restrictive you should add "Deny from all" in there too, just make sure you add "Allow from all" in your home directory section )

Why the fix works

.htaccess files are processed from the current folder to the parent folder and all the way up to the root folder (of the disk!). Setting "AllowOverride None" on /var/www/mysite/media meant that in the media folder and down, no .htacces files are processed, but they still inherit from the parent folders.

Setting the AllowOverride None on the root folder meant that no .htaccess files are processed except those in the folders that I explicitly allow.