Skip to content

.htaccess pitfalls — why we move rules into the vhost

How .htaccess parsing works per request, what the performance cost is, and the audit pattern for moving rules into the vhost without breaking deploys.

.htaccess looks convenient — drop a file in a directory and Apache picks up rewrite rules without a config reload. The cost is that Apache walks the entire directory tree from DocumentRoot to the requested file on every request, looking for .htaccess files at each level, and re-parses them every time. On a busy box, that is hundreds of unnecessary stat() calls per second. This article is the case for AllowOverride None and the migration pattern from .htaccess to vhost rules.

How to verify

Find out which sites still use .htaccess and what the AllowOverride policy actually is:

grep -rE 'AllowOverride' /etc/apache2/sites-enabled/ /etc/apache2/conf-enabled/ /etc/apache2/apache2.conf
find /var/www -name '.htaccess' -type f
strace -e trace=openat -p $(pgrep -f apache2 | head -1) 2>&1 | grep -c htaccess
sudo apache2ctl -M | grep rewrite

The strace line, run for a few seconds while traffic flows, gives you the actual rate at which Apache is looking up .htaccess files. On a site that does not need it, that number should be zero.

What’s happening

When a request arrives at /var/www/example.com/public/blog/post/123, and AllowOverride is not None, Apache walks every directory in that path and looks for .htaccess:

/var/www/.htaccess
/var/www/example.com/.htaccess
/var/www/example.com/public/.htaccess
/var/www/example.com/public/blog/.htaccess
/var/www/example.com/public/blog/post/.htaccess
/var/www/example.com/public/blog/post/123/.htaccess

Each level: one stat() call. If a file exists, Apache opens it, parses it, and merges its directives into the request’s effective config. None of this is cached across requests by default — every request pays the full traversal cost.

The merge order matters too. A RewriteRule in a higher directory applies after one in a lower directory, and AllowOverride None higher up does not prevent a lower-level .htaccess from being honored if a closer directory explicitly enables it.

The performance impact is small per request — fractions of a millisecond. The cumulative impact on a high-traffic site is measurable: shop floor measurements typically show 5-15% throughput recovery on a busy WordPress/Magento box when AllowOverride None replaces AllowOverride All with the rules lifted into the vhost.

The procedure

Migrating from .htaccess to vhost is a four-step pattern. Do not skip the audit step — the most common failure is a forgotten .htaccess rule that ships with a CMS update.

  1. Inventory every .htaccess file under the DocumentRoot.

    sudo find /var/www -name '.htaccess' -type f -print -exec wc -l {} \;

    For each file, capture its content and the directory it lives in. CMSes (WordPress, Magento, Joomla) ship their own .htaccess and rewrite it on update; document this so the vhost merge does not get overwritten silently.

  2. Move the rules into the vhost. Wrap each rule in a <Directory> block matching the original location:

    <Directory /var/www/example.com/public>
        AllowOverride None
        Require all granted
    
        # Lifted from .htaccess
        RewriteEngine On
        RewriteBase /
        RewriteRule ^index\.php$ - [L]
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule . /index.php [L]
    </Directory>

    Note RewriteBase / — in vhost scope, the URL path includes the leading slash; in .htaccess, it does not. Test rules after the move.

  3. Set AllowOverride None globally. Override per-vhost only if a specific site truly cannot move:

    # In /etc/apache2/apache2.conf, the default <Directory /var/www>
    <Directory /var/www>
        Options Indexes FollowSymLinks
        AllowOverride None
        Require all granted
    </Directory>
  4. Configtest and reload. Then run the verification curls — same URLs, before and after, expect identical responses:

    sudo apache2ctl configtest
    sudo systemctl reload apache2
    curl -sI https://example.com/old-blog-url
    curl -sI https://example.com/wp-admin/
  5. For CMS-managed .htaccess files, keep them on disk but never honor them. With AllowOverride None, the file exists but Apache ignores it. The CMS will rewrite it on update; harmless because it has no effect. The active rules live in the vhost.

  6. Document the migration. A comment block in the vhost explaining “these rules were lifted from /var/www/example.com/public/.htaccess on 2026-06-02; do not edit the .htaccess file, it is no longer authoritative” is what saves the next person from confusion.

Common pitfalls

  • A .htaccess rule that hooks into a directory deep in the tree (e.g. /wp-admin/.htaccess with IP-based protection) is invisible at the top level — find catches it, manual review does not.
  • AllowOverride None does not stop Apache from doing the stat() walk in older versions; modern Apache (2.4+) skips the walk when the override is None throughout the path. Verify your version supports the optimization.
  • WordPress’s .htaccess includes a fenced block marked # BEGIN WordPress and # END WordPress — anything inside is rewritten on permalink saves; comments outside are preserved.
  • The migration breaks if any vhost-scope RewriteRule accidentally has the [L] flag and the rule above it relies on the lower-priority .htaccess rule firing. Test every redirect explicitly.
  • A team that does not control the application code will lose the migration when a CMS upgrade ships fresh .htaccess instructions — keep AllowOverride None and ignore the file’s regeneration.

Stack Harbor audits Apache fleets for .htaccess reliance as part of the standard hand-off — every site we onboard either gets its rules lifted into the vhost, or has a documented exception with the performance trade-off accepted. If your boxes have hundreds of .htaccess files across vendor and bespoke sites, the audit is part of the Managed Operations baseline.