Merging and Inheritance

Merge Order, Inheritance and Overriding a Parent Directory

Contexts and Merge Order placed .htaccess in the overall order: it merges with its own level's <Directory>, before <DirectoryMatch>, <Files>, <Location> and <If>. Within the walk, deeper files override shallower ones, but each module decides what that means. DirectoryIndex and other single values replace. Header lines accumulate, parent first. Options -X edits the inherited set. mod_rewrite resets: a child with its own RewriteEngine On starts with no rules. Two files under AllowOverride All show all three:

The parent and the child fileApache config
# /var/www/example/.htaccess
Options -Indexes
Header set X-Level "root"
Header append X-Trail "root"
RewriteEngine On
RewriteRule ^(.*)old\.html$ $1new.html [L]
# /var/www/example/docs/.htaccess
Header set X-Level "docs"
Header append X-Trail "docs"
RewriteEngine On
RewriteRule ^legacy\.html$ current.html [L]
One request per behaviorApache config
for u in /old.html /docs/legacy.html /docs/old.html; do
  printf '%-17s ' $u
  curl -si localhost:8104$u | tr -d '\r' | grep -E '^HTTP|^X-|^<p>' | paste -sd' '
done
Output
/old.html         HTTP/1.1 200 OK X-Level: root X-Trail: root <p>new</p>
/docs/legacy.html HTTP/1.1 200 OK X-Level: docs X-Trail: root, docs <p>docs current</p>
/docs/old.html    HTTP/1.1 404 Not Found <p>The requested URL was not found on this server.</p>

The child's set won, append built a list, and /docs/ returned 403 under the parent's -Indexes. The parent's rewrite was lost: LogLevel rewrite:trace3 logged only the child's pattern, then pass through. With RewriteOptions Inherit in the child, the parent's rules run after its own and the request returns docs/new.html, because an inherited rule sees the child's stripped path (RewriteBase):

Output of 29
[perdir /var/www/example/docs/] applying pattern '^(.*)old\\.html$' to uri 'old.html'
[perdir /var/www/example/docs/] rewrite 'old.html' -> 'new.html'
[perdir /var/www/example/docs/] add per-dir prefix: new.html -> /var/www/example/docs/new.html

InheritBefore runs them first instead; InheritDown in the parent pushes them into every child.